Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DLinq / Dlinq / SqlClient / SqlConnectionManager.cs / 1305376 / 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 bool disposeConnection; // should we dispose this connection when the context is disposed? private DbTransaction transaction; private Transaction systemTransaction; private SqlInfoMessageEventHandler infoMessagehandler; private Listusers; private int maxUsers; internal SqlConnectionManager(IProvider provider, DbConnection con, int maxUsers, bool disposeConnection) { this.provider = provider; this.connection = con; this.maxUsers = maxUsers; this.infoMessagehandler = new SqlInfoMessageEventHandler(this.OnInfoMessage); this.users = new List (maxUsers); this.disposeConnection = disposeConnection; } 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(); } // If we created the connection, we need to dispose it even if the user explicitly // opened it using the Connection property on the context. if (this.connection != null && this.disposeConnection) { this.connection.Dispose(); this.connection = null; } } 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
- XmlSchemaImport.cs
- OverlappedAsyncResult.cs
- DataSvcMapFile.cs
- Color.cs
- SQLBytesStorage.cs
- LineSegment.cs
- BaseTreeIterator.cs
- XmlSignatureManifest.cs
- GAC.cs
- DataGridViewElement.cs
- CollectionType.cs
- SchemaExporter.cs
- ObjRef.cs
- WebBrowser.cs
- BackStopAuthenticationModule.cs
- SystemIPInterfaceStatistics.cs
- XPathNodeIterator.cs
- SqlDataSourceConfigureSelectPanel.cs
- FontResourceCache.cs
- Validator.cs
- ApplicationCommands.cs
- AuthenticationManager.cs
- OdbcConnectionPoolProviderInfo.cs
- PixelShader.cs
- AttributeSetAction.cs
- NativeMethods.cs
- DictionaryManager.cs
- WsdlInspector.cs
- TrayIconDesigner.cs
- TextEditorTables.cs
- HwndSourceKeyboardInputSite.cs
- PreviewPrintController.cs
- ConfigsHelper.cs
- precedingsibling.cs
- StylusEventArgs.cs
- TypeBrowser.xaml.cs
- FileChangesMonitor.cs
- TextBoxDesigner.cs
- IApplicationTrustManager.cs
- HttpRuntimeSection.cs
- AsyncOperation.cs
- Binding.cs
- processwaithandle.cs
- PackageRelationshipSelector.cs
- Comparer.cs
- SqlTypeConverter.cs
- LoginView.cs
- PlainXmlWriter.cs
- CellParaClient.cs
- BaseDataListPage.cs
- ZipIOZip64EndOfCentralDirectoryBlock.cs
- DataSourceSelectArguments.cs
- PageWrapper.cs
- MostlySingletonList.cs
- SafeFileHandle.cs
- PowerStatus.cs
- SoapBinding.cs
- CounterCreationData.cs
- TextFormatterContext.cs
- VsPropertyGrid.cs
- BaseDataList.cs
- itemelement.cs
- XmlArrayAttribute.cs
- comcontractssection.cs
- NavigationCommands.cs
- DynamicActivityProperty.cs
- DataGridViewEditingControlShowingEventArgs.cs
- FactoryRecord.cs
- ClientUriBehavior.cs
- TypeLoadException.cs
- TrackBar.cs
- ForEach.cs
- RemotingServices.cs
- PeerResolverElement.cs
- DateTimeFormatInfo.cs
- ParentQuery.cs
- HierarchicalDataBoundControl.cs
- HttpModuleCollection.cs
- StateRuntime.cs
- StorageConditionPropertyMapping.cs
- RegexTypeEditor.cs
- LogWriteRestartAreaState.cs
- TableLayoutRowStyleCollection.cs
- DisplayToken.cs
- BookmarkList.cs
- PrinterResolution.cs
- VideoDrawing.cs
- TracedNativeMethods.cs
- BCryptHashAlgorithm.cs
- NamespaceInfo.cs
- Control.cs
- ServiceModelActivity.cs
- SiteMapSection.cs
- ComNativeDescriptor.cs
- WindowsRebar.cs
- DispatcherHookEventArgs.cs
- BitmapCodecInfoInternal.cs
- CollectionChangeEventArgs.cs
- FloaterBaseParagraph.cs
- NominalTypeEliminator.cs