Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DLinq / Dlinq / SqlClient / SqlConnectionManager.cs / 1 / SqlConnectionManager.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using System.IO;
using System.Reflection;
using System.Text;
using System.Transactions;
namespace System.Data.Linq.SqlClient {
using System.Data.Linq;
using System.Data.Linq.Provider;
internal class SqlConnectionManager : IConnectionManager {
private IProvider provider;
private DbConnection connection;
private bool autoClose;
private DbTransaction transaction;
private Transaction systemTransaction;
private SqlInfoMessageEventHandler infoMessagehandler;
private List users;
private int maxUsers;
internal SqlConnectionManager(IProvider provider, DbConnection con, int maxUsers) {
this.provider = provider;
this.connection = con;
this.maxUsers = maxUsers;
this.infoMessagehandler = new SqlInfoMessageEventHandler(this.OnInfoMessage);
this.users = new List(maxUsers);
}
public DbConnection UseConnection(IConnectionUser user) {
if (user == null) {
throw Error.ArgumentNull("user");
}
if (this.connection.State == ConnectionState.Closed) {
this.connection.Open();
this.autoClose = true;
this.AddInfoMessageHandler();
if (System.Transactions.Transaction.Current != null) {
System.Transactions.Transaction.Current.TransactionCompleted += this.OnTransactionCompleted;
}
}
if (this.transaction == null && System.Transactions.Transaction.Current != null &&
System.Transactions.Transaction.Current != systemTransaction) {
this.ClearConnection();
systemTransaction = System.Transactions.Transaction.Current;
this.connection.EnlistTransaction(System.Transactions.Transaction.Current);
}
if (this.users.Count == this.maxUsers) {
this.BootUser(this.users[0]);
}
this.users.Add(user);
return this.connection;
}
private void BootUser(IConnectionUser user) {
bool saveAutoClose = this.autoClose;
this.autoClose = false;
int index = this.users.IndexOf(user);
if (index >= 0) {
this.users.RemoveAt(index);
}
user.CompleteUse();
this.autoClose = saveAutoClose;
}
internal DbConnection Connection {
get { return this.connection; }
}
internal int MaxUsers {
get { return this.maxUsers; }
}
internal void DisposeConnection() {
// only close this guy if we opened it in the first place
if (this.autoClose) {
this.CloseConnection();
}
}
internal void ClearConnection() {
while (this.users.Count > 0) {
this.BootUser(this.users[0]);
}
}
internal bool AutoClose {
get { return this.autoClose; }
set { this.autoClose = value; }
}
internal DbTransaction Transaction {
get { return this.transaction; }
set {
if (value != this.transaction) {
if (value != null) {
if (this.connection != value.Connection) {
throw Error.TransactionDoesNotMatchConnection();
}
}
this.transaction = value;
}
}
}
public void ReleaseConnection(IConnectionUser user) {
if (user == null) {
throw Error.ArgumentNull("user");
}
int index = this.users.IndexOf(user);
if (index >= 0) {
this.users.RemoveAt(index);
}
if (this.users.Count == 0 && this.autoClose && this.transaction == null && System.Transactions.Transaction.Current == null) {
this.CloseConnection();
}
}
private void CloseConnection() {
if (this.connection != null && this.connection.State != ConnectionState.Closed) {
this.connection.Close();
}
this.RemoveInfoMessageHandler();
this.autoClose = false;
}
private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args) {
if (this.provider.Log != null) {
this.provider.Log.WriteLine(Strings.LogGeneralInfoMessage(args.Source, args.Message));
}
}
private void OnTransactionCompleted(object sender, System.Transactions.TransactionEventArgs args) {
if (this.users.Count == 0 && this.autoClose) {
this.CloseConnection();
}
}
private void AddInfoMessageHandler() {
SqlConnection scon = this.connection as SqlConnection;
if (scon != null) {
scon.InfoMessage += this.infoMessagehandler;
}
}
private void RemoveInfoMessageHandler() {
SqlConnection scon = this.connection as SqlConnection;
if (scon != null) {
scon.InfoMessage -= this.infoMessagehandler;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using System.IO;
using System.Reflection;
using System.Text;
using System.Transactions;
namespace System.Data.Linq.SqlClient {
using System.Data.Linq;
using System.Data.Linq.Provider;
internal class SqlConnectionManager : IConnectionManager {
private IProvider provider;
private DbConnection connection;
private bool autoClose;
private DbTransaction transaction;
private Transaction systemTransaction;
private SqlInfoMessageEventHandler infoMessagehandler;
private List users;
private int maxUsers;
internal SqlConnectionManager(IProvider provider, DbConnection con, int maxUsers) {
this.provider = provider;
this.connection = con;
this.maxUsers = maxUsers;
this.infoMessagehandler = new SqlInfoMessageEventHandler(this.OnInfoMessage);
this.users = new List(maxUsers);
}
public DbConnection UseConnection(IConnectionUser user) {
if (user == null) {
throw Error.ArgumentNull("user");
}
if (this.connection.State == ConnectionState.Closed) {
this.connection.Open();
this.autoClose = true;
this.AddInfoMessageHandler();
if (System.Transactions.Transaction.Current != null) {
System.Transactions.Transaction.Current.TransactionCompleted += this.OnTransactionCompleted;
}
}
if (this.transaction == null && System.Transactions.Transaction.Current != null &&
System.Transactions.Transaction.Current != systemTransaction) {
this.ClearConnection();
systemTransaction = System.Transactions.Transaction.Current;
this.connection.EnlistTransaction(System.Transactions.Transaction.Current);
}
if (this.users.Count == this.maxUsers) {
this.BootUser(this.users[0]);
}
this.users.Add(user);
return this.connection;
}
private void BootUser(IConnectionUser user) {
bool saveAutoClose = this.autoClose;
this.autoClose = false;
int index = this.users.IndexOf(user);
if (index >= 0) {
this.users.RemoveAt(index);
}
user.CompleteUse();
this.autoClose = saveAutoClose;
}
internal DbConnection Connection {
get { return this.connection; }
}
internal int MaxUsers {
get { return this.maxUsers; }
}
internal void DisposeConnection() {
// only close this guy if we opened it in the first place
if (this.autoClose) {
this.CloseConnection();
}
}
internal void ClearConnection() {
while (this.users.Count > 0) {
this.BootUser(this.users[0]);
}
}
internal bool AutoClose {
get { return this.autoClose; }
set { this.autoClose = value; }
}
internal DbTransaction Transaction {
get { return this.transaction; }
set {
if (value != this.transaction) {
if (value != null) {
if (this.connection != value.Connection) {
throw Error.TransactionDoesNotMatchConnection();
}
}
this.transaction = value;
}
}
}
public void ReleaseConnection(IConnectionUser user) {
if (user == null) {
throw Error.ArgumentNull("user");
}
int index = this.users.IndexOf(user);
if (index >= 0) {
this.users.RemoveAt(index);
}
if (this.users.Count == 0 && this.autoClose && this.transaction == null && System.Transactions.Transaction.Current == null) {
this.CloseConnection();
}
}
private void CloseConnection() {
if (this.connection != null && this.connection.State != ConnectionState.Closed) {
this.connection.Close();
}
this.RemoveInfoMessageHandler();
this.autoClose = false;
}
private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args) {
if (this.provider.Log != null) {
this.provider.Log.WriteLine(Strings.LogGeneralInfoMessage(args.Source, args.Message));
}
}
private void OnTransactionCompleted(object sender, System.Transactions.TransactionEventArgs args) {
if (this.users.Count == 0 && this.autoClose) {
this.CloseConnection();
}
}
private void AddInfoMessageHandler() {
SqlConnection scon = this.connection as SqlConnection;
if (scon != null) {
scon.InfoMessage += this.infoMessagehandler;
}
}
private void RemoveInfoMessageHandler() {
SqlConnection scon = this.connection as SqlConnection;
if (scon != null) {
scon.InfoMessage -= this.infoMessagehandler;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataSourceControlBuilder.cs
- CollectionDataContract.cs
- WeakHashtable.cs
- OpCellTreeNode.cs
- AssemblyResourceLoader.cs
- XmlValidatingReader.cs
- SizeConverter.cs
- SelectorItemAutomationPeer.cs
- PeerCollaboration.cs
- ClientConfigurationSystem.cs
- dataprotectionpermission.cs
- QuerySelectOp.cs
- EventManager.cs
- recordstatefactory.cs
- AmbiguousMatchException.cs
- ComboBoxItem.cs
- BinaryCommonClasses.cs
- ViewKeyConstraint.cs
- SizeChangedEventArgs.cs
- wmiutil.cs
- RegexReplacement.cs
- MarkupExtensionReturnTypeAttribute.cs
- QilBinary.cs
- QueryMath.cs
- WsatProxy.cs
- LineServicesCallbacks.cs
- AsyncInvokeOperation.cs
- QueueSurrogate.cs
- SpinLock.cs
- Certificate.cs
- FormatStringEditor.cs
- ACE.cs
- StoreContentChangedEventArgs.cs
- XmlDocument.cs
- BrowserCapabilitiesFactoryBase.cs
- XmlSchemaObject.cs
- CompensatableTransactionScopeActivityDesigner.cs
- SqlDataSourceSummaryPanel.cs
- CompressStream.cs
- SBCSCodePageEncoding.cs
- DataViewSetting.cs
- ListItemsCollectionEditor.cs
- HttpValueCollection.cs
- WebHostScriptMappingsInstallComponent.cs
- WmiInstallComponent.cs
- XPathNavigatorKeyComparer.cs
- StaticResourceExtension.cs
- ReachDocumentReferenceSerializer.cs
- TextSyndicationContent.cs
- InstanceLockLostException.cs
- SqlConnection.cs
- AssemblyHash.cs
- DockingAttribute.cs
- CollectionView.cs
- CreateUserWizard.cs
- UrlUtility.cs
- SqlEnums.cs
- MissingSatelliteAssemblyException.cs
- BinaryUtilClasses.cs
- WebRequestModulesSection.cs
- WinOEToolBoxItem.cs
- EastAsianLunisolarCalendar.cs
- ComboBoxItem.cs
- TextDecorationCollection.cs
- HttpCachePolicyElement.cs
- XamlVector3DCollectionSerializer.cs
- MaterialGroup.cs
- SafeIUnknown.cs
- FamilyMapCollection.cs
- WindowsIPAddress.cs
- EditorPartCollection.cs
- WebPartDisplayModeCollection.cs
- AstNode.cs
- UrlMappingCollection.cs
- ToggleButton.cs
- TransformConverter.cs
- IgnoreSection.cs
- EntityDataSourceContainerNameConverter.cs
- DataSourceCache.cs
- InstancePersistenceCommandException.cs
- Substitution.cs
- StringStorage.cs
- CompositeDesignerAccessibleObject.cs
- FaultCallbackWrapper.cs
- InvokePattern.cs
- XPathNavigatorKeyComparer.cs
- PropertyItem.cs
- PageThemeBuildProvider.cs
- InheritanceAttribute.cs
- IHttpResponseInternal.cs
- SafeTimerHandle.cs
- TransformedBitmap.cs
- PropertyToken.cs
- SafeThemeHandle.cs
- FactoryMaker.cs
- HttpHandlerAction.cs
- LOSFormatter.cs
- UInt64Storage.cs
- SerializationEventsCache.cs
- PenThread.cs