1 //------------------------------------------------------------------------------
2 // <copyright file="SqlConnection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
9 [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Data.DataSetExtensions, PublicKey="+AssemblyRef.EcmaPublicKeyFull)] // DevDiv Bugs 92166
11 namespace System.Data.SqlClient
14 using System.Collections;
15 using System.Collections.Concurrent;
16 using System.Collections.Generic;
17 using System.Collections.ObjectModel;
18 using System.Configuration.Assemblies;
19 using System.ComponentModel;
21 using System.Data.Common;
22 using System.Data.ProviderBase;
23 using System.Data.Sql;
24 using System.Data.SqlTypes;
25 using System.Diagnostics;
26 using System.Globalization;
29 using System.Runtime.CompilerServices;
30 using System.Runtime.ConstrainedExecution;
31 using System.Runtime.InteropServices;
32 using System.Runtime.Remoting;
33 using System.Runtime.Serialization.Formatters;
35 using System.Threading;
36 using System.Threading.Tasks;
37 using System.Security;
38 using System.Security.Permissions;
39 using System.Reflection;
40 using System.Runtime.Versioning;
42 using Microsoft.SqlServer.Server;
43 using System.Security.Principal;
44 using System.Diagnostics.CodeAnalysis;
46 [DefaultEvent("InfoMessage")]
47 public sealed partial class SqlConnection: DbConnection, ICloneable {
49 static private readonly object EventInfoMessage = new object();
51 // System column encryption key store providers are added by default
52 static private readonly Dictionary<string, SqlColumnEncryptionKeyStoreProvider> _SystemColumnEncryptionKeyStoreProviders
53 = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase)
55 {SqlColumnEncryptionCertificateStoreProvider.ProviderName, new SqlColumnEncryptionCertificateStoreProvider()}
59 /// Custom provider list should be provided by the user. We shallow copy the user supplied dictionary into a ReadOnlyDictionary.
60 /// Custom provider list can only supplied once per application.
62 static private ReadOnlyDictionary<string, SqlColumnEncryptionKeyStoreProvider> _CustomColumnEncryptionKeyStoreProviders;
64 // Lock to control setting of _CustomColumnEncryptionKeyStoreProviders
65 static private readonly Object _CustomColumnEncryptionKeyProvidersLock = new Object();
68 /// Dictionary object holding trusted key paths for various SQL Servers.
69 /// Key to the dictionary is a SQL Server Name
70 /// IList contains a list of trusted key paths.
72 static private readonly ConcurrentDictionary<string, IList<string>> _ColumnEncryptionTrustedMasterKeyPaths
73 = new ConcurrentDictionary<string, IList<string>>(concurrencyLevel: 4 * Environment.ProcessorCount /* default value in ConcurrentDictionary*/,
75 comparer: StringComparer.OrdinalIgnoreCase);
79 ResCategoryAttribute(Res.DataCategory_Data),
80 ResDescriptionAttribute(Res.TCE_SqlConnection_TrustedColumnMasterKeyPaths),
82 static public IDictionary<string, IList<string>> ColumnEncryptionTrustedMasterKeyPaths
86 return _ColumnEncryptionTrustedMasterKeyPaths;
91 /// This function should only be called once in an app. This does shallow copying of the dictionary so that
92 /// the app cannot alter the custom provider list once it has been set.
96 /// Dictionary<string, SqlColumnEncryptionKeyStoreProvider> customKeyStoreProviders = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
97 /// MySqlClientHSMProvider myProvider = new MySqlClientHSMProvider();
98 /// customKeyStoreProviders.Add(@"HSM Provider", myProvider);
99 /// SqlConnection.RegisterColumnEncryptionKeyStoreProviders(customKeyStoreProviders);
101 /// <param name="customProviders">Custom column encryption key provider dictionary</param>
102 static public void RegisterColumnEncryptionKeyStoreProviders(IDictionary<string, SqlColumnEncryptionKeyStoreProvider> customProviders)
105 // Return when the provided dictionary is null.
106 if (customProviders == null)
108 throw SQL.NullCustomKeyStoreProviderDictionary();
111 // Validate that custom provider list doesn't contain any of system provider list
112 foreach (string key in customProviders.Keys)
114 // Validate the provider name
116 // Check for null or empty
117 if (string.IsNullOrWhiteSpace(key))
119 throw SQL.EmptyProviderName();
122 // Check if the name starts with MSSQL_, since this is reserved namespace for system providers.
123 if (key.StartsWith(ADP.ColumnEncryptionSystemProviderNamePrefix, StringComparison.InvariantCultureIgnoreCase))
125 throw SQL.InvalidCustomKeyStoreProviderName(key, ADP.ColumnEncryptionSystemProviderNamePrefix);
128 // Validate the provider value
129 if (customProviders[key] == null)
131 throw SQL.NullProviderValue(key);
135 lock (_CustomColumnEncryptionKeyProvidersLock)
137 // Provider list can only be set once
138 if (_CustomColumnEncryptionKeyStoreProviders != null)
140 throw SQL.CanOnlyCallOnce();
143 // Create a temporary dictionary and then add items from the provided dictionary.
144 // Dictionary constructor does shallow copying by simply copying the provider name and provider reference pairs
145 // in the provided customerProviders dictionary.
146 Dictionary<string, SqlColumnEncryptionKeyStoreProvider> customColumnEncryptionKeyStoreProviders =
147 new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>(customProviders, StringComparer.OrdinalIgnoreCase);
149 // Set the dictionary to the ReadOnly dictionary.
150 _CustomColumnEncryptionKeyStoreProviders = new ReadOnlyDictionary<string, SqlColumnEncryptionKeyStoreProvider>(customColumnEncryptionKeyStoreProviders);
155 /// This function walks through both system and custom column encryption key store providers and returns an object if found.
157 /// <param name="providerName">Provider Name to be searched in System Provider diction and Custom provider dictionary.</param>
158 /// <param name="columnKeyStoreProvider">If the provider is found, returns the corresponding SqlColumnEncryptionKeyStoreProvider instance.</param>
159 /// <returns>true if the provider is found, else returns false</returns>
160 static internal bool TryGetColumnEncryptionKeyStoreProvider(string providerName, out SqlColumnEncryptionKeyStoreProvider columnKeyStoreProvider) {
161 Debug.Assert(!string.IsNullOrWhiteSpace(providerName), "Provider name is invalid");
163 // Initialize the out parameter
164 columnKeyStoreProvider = null;
166 // Search in the sytem provider list.
167 if (_SystemColumnEncryptionKeyStoreProviders.TryGetValue(providerName, out columnKeyStoreProvider))
172 lock (_CustomColumnEncryptionKeyProvidersLock)
174 // If custom provider is not set, then return false
175 if (_CustomColumnEncryptionKeyStoreProviders == null)
180 // Search in the custom provider list
181 return _CustomColumnEncryptionKeyStoreProviders.TryGetValue(providerName, out columnKeyStoreProvider);
186 /// This function returns a list of system provider dictionary currently supported by this driver.
188 /// <returns>Combined list of provider names</returns>
189 static internal List<string> GetColumnEncryptionSystemKeyStoreProviders() {
190 HashSet<string> providerNames = new HashSet<string>(_SystemColumnEncryptionKeyStoreProviders.Keys);
191 return providerNames.ToList();
195 /// This function returns a list of custom provider dictionary currently registered.
197 /// <returns>Combined list of provider names</returns>
198 static internal List<string> GetColumnEncryptionCustomKeyStoreProviders() {
199 if(_CustomColumnEncryptionKeyStoreProviders != null)
201 HashSet<string> providerNames = new HashSet<string>(_CustomColumnEncryptionKeyStoreProviders.Keys);
202 return providerNames.ToList();
205 return new List<string>();
208 private SqlDebugContext _sdc; // SQL Debugging support
210 private bool _AsyncCommandInProgress;
212 // SQLStatistics support
213 internal SqlStatistics _statistics;
214 private bool _collectstats;
216 private bool _fireInfoMessageEventOnUserErrors; // False by default
218 // root task associated with current async invocation
219 Tuple<TaskCompletionSource<DbConnectionInternal>, Task> _currentCompletion;
221 private SqlCredential _credential; // SQL authentication password stored in SecureString
222 private string _connectionString;
223 private int _connectRetryCount;
225 private string _accessToken; // Access Token to be used for token based authententication
227 // connection resiliency
228 private object _reconnectLock = new object();
229 internal Task _currentReconnectionTask;
230 private Task _asyncWaitingForReconnection; // current async task waiting for reconnection in non-MARS connections
231 private Guid _originalConnectionId = Guid.Empty;
232 private CancellationTokenSource _reconnectionCancellationSource;
233 internal SessionData _recoverySessionData;
234 internal WindowsIdentity _lastIdentity;
235 internal WindowsIdentity _impersonateIdentity;
236 private int _reconnectCount;
238 public SqlConnection(string connectionString) : this(connectionString, null) {
241 public SqlConnection(string connectionString, SqlCredential credential) : this() {
242 ConnectionString = connectionString; // setting connection string first so that ConnectionOption is available
243 if (credential != null)
245 // The following checks are necessary as setting Credential property will call CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential
246 // CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential it will throw InvalidOperationException rather than Arguemtn exception
247 // Need to call setter on Credential property rather than setting _credential directly as pool groups need to be checked
248 SqlConnectionString connectionOptions = (SqlConnectionString) ConnectionOptions;
249 if (UsesClearUserIdOrPassword(connectionOptions))
251 throw ADP.InvalidMixedArgumentOfSecureAndClearCredential();
254 if (UsesIntegratedSecurity(connectionOptions))
256 throw ADP.InvalidMixedArgumentOfSecureCredentialAndIntegratedSecurity();
259 if (UsesContextConnection(connectionOptions))
261 throw ADP.InvalidMixedArgumentOfSecureCredentialAndContextConnection();
264 if (UsesActiveDirectoryIntegrated(connectionOptions))
266 throw SQL.SettingCredentialWithIntegratedArgument();
269 Credential = credential;
272 // credential == null: we should not set "Credential" as this will do additional validation check and
273 // checking pool groups which is not necessary. All necessary operation is already done by calling "ConnectionString = connectionString"
274 CacheConnectionStringProperties();
277 private SqlConnection(SqlConnection connection) { // Clone
278 GC.SuppressFinalize(this);
279 CopyFrom(connection);
280 _connectionString = connection._connectionString;
281 if (connection._credential != null)
283 SecureString password = connection._credential.Password.Copy();
284 password.MakeReadOnly();
285 _credential = new SqlCredential(connection._credential.UserId, password);
287 _accessToken = connection._accessToken;
288 CacheConnectionStringProperties();
291 // This method will be called once connection string is set or changed.
292 private void CacheConnectionStringProperties() {
293 SqlConnectionString connString = ConnectionOptions as SqlConnectionString;
294 if (connString != null) {
295 _connectRetryCount = connString.ConnectRetryCount;
303 // used to start/stop collection of statistics data and do verify the current state
305 // devnote: start/stop should not performed using a property since it requires execution of code
308 // set the internal flag (_statisticsEnabled) to true.
309 // Create a new SqlStatistics object if not already there.
310 // connect the parser to the object.
311 // if there is no parser at this time we need to connect it after creation.
316 ResCategoryAttribute(Res.DataCategory_Data),
317 ResDescriptionAttribute(Res.SqlConnection_StatisticsEnabled),
319 public bool StatisticsEnabled {
321 return (_collectstats);
324 if (IsContextConnection) {
326 throw SQL.NotAvailableOnContextConnection();
332 if (ConnectionState.Open == State) {
333 if (null == _statistics) {
334 _statistics = new SqlStatistics();
335 ADP.TimerCurrent(out _statistics._openTimestamp);
337 // set statistics on the parser
339 Debug.Assert(Parser != null, "Where's the parser?");
340 Parser.Statistics = _statistics;
345 if (null != _statistics) {
346 if (ConnectionState.Open == State) {
347 // remove statistics from parser
349 TdsParser parser = Parser;
350 Debug.Assert(parser != null, "Where's the parser?");
351 parser.Statistics = null;
352 ADP.TimerCurrent(out _statistics._closeTimestamp);
356 this._collectstats = value;
361 internal bool AsyncCommandInProgress {
363 return (_AsyncCommandInProgress);
365 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
367 _AsyncCommandInProgress = value;
371 internal bool IsContextConnection {
373 SqlConnectionString opt = (SqlConnectionString)ConnectionOptions;
374 return UsesContextConnection(opt);
379 /// Is this connection using column encryption ?
381 internal bool IsColumnEncryptionSettingEnabled {
383 SqlConnectionString opt = (SqlConnectionString)ConnectionOptions;
384 return opt != null ? opt.ColumnEncryptionSetting == SqlConnectionColumnEncryptionSetting.Enabled : false;
388 // Is this connection is a Context Connection?
389 private bool UsesContextConnection(SqlConnectionString opt)
391 return opt != null ? opt.ContextConnection : false;
394 private bool UsesActiveDirectoryIntegrated(SqlConnectionString opt)
396 return opt != null ? opt.Authentication == SqlAuthenticationMethod.ActiveDirectoryIntegrated : false;
399 private bool UsesAuthentication(SqlConnectionString opt) {
400 return opt != null ? opt.Authentication != SqlAuthenticationMethod.NotSpecified : false;
403 // Does this connection uses Integrated Security?
404 private bool UsesIntegratedSecurity(SqlConnectionString opt) {
405 return opt != null ? opt.IntegratedSecurity : false;
408 // Does this connection uses old style of clear userID or Password in connection string?
409 private bool UsesClearUserIdOrPassword(SqlConnectionString opt) {
412 result = (!ADP.IsEmpty(opt.UserID) || !ADP.IsEmpty(opt.Password));
417 internal SqlConnectionString.TransactionBindingEnum TransactionBinding {
419 return ((SqlConnectionString)ConnectionOptions).TransactionBinding;
423 internal SqlConnectionString.TypeSystem TypeSystem {
425 return ((SqlConnectionString)ConnectionOptions).TypeSystemVersion;
429 internal Version TypeSystemAssemblyVersion {
431 return ((SqlConnectionString)ConnectionOptions).TypeSystemAssemblyVersion;
435 internal int ConnectRetryInterval {
437 return ((SqlConnectionString)ConnectionOptions).ConnectRetryInterval;
441 override protected DbProviderFactory DbProviderFactory {
443 return SqlClientFactory.Instance;
447 // AccessToken: To be used for token based authentication
450 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
451 ResDescriptionAttribute(Res.SqlConnection_AccessToken),
453 public string AccessToken {
455 string result = _accessToken;
456 // When a connection is connecting or is ever opened, make AccessToken available only if "Persist Security Info" is set to true
457 // otherwise, return null
458 SqlConnectionString connectionOptions = (SqlConnectionString)UserConnectionOptions;
459 if (InnerConnection.ShouldHidePassword && connectionOptions != null && !connectionOptions.PersistSecurityInfo) {
466 // If a connection is connecting or is ever opened, AccessToken cannot be set
467 if (!InnerConnection.AllowSetConnectionString) {
468 throw ADP.OpenConnectionPropertySet("AccessToken", InnerConnection.State);
472 // Check if the usage of AccessToken has any conflict with the keys used in connection string and credential
473 CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessToken((SqlConnectionString)ConnectionOptions);
476 _accessToken = value;
477 // Need to call ConnectionString_Set to do proper pool group check
478 ConnectionString_Set(new SqlConnectionPoolKey(_connectionString, credential: _credential, accessToken: _accessToken));
484 #pragma warning disable 618 // ignore obsolete warning about RecommendedAsConfigurable to use SettingsBindableAttribute
485 RecommendedAsConfigurable(true),
486 #pragma warning restore 618
487 SettingsBindableAttribute(true),
488 RefreshProperties(RefreshProperties.All),
489 ResCategoryAttribute(Res.DataCategory_Data),
490 Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
491 ResDescriptionAttribute(Res.SqlConnection_ConnectionString),
493 override public string ConnectionString {
495 return ConnectionString_Get();
498 if(_credential != null || _accessToken != null) {
499 SqlConnectionString connectionOptions = new SqlConnectionString(value);
500 if(_credential != null) {
501 // Check for Credential being used with Authentication=ActiveDirectoryIntegrated. Since a different error string is used
502 // for this case in ConnectionString setter vs in Credential setter, check for this error case before calling
503 // CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential, which is common to both setters.
504 if(UsesActiveDirectoryIntegrated(connectionOptions)) {
505 throw SQL.SettingIntegratedWithCredential();
508 CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential(connectionOptions);
510 else if(_accessToken != null) {
511 CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessToken(connectionOptions);
514 ConnectionString_Set(new SqlConnectionPoolKey(value, _credential, _accessToken));
515 _connectionString = value; // Change _connectionString value only after value is validated
516 CacheConnectionStringProperties();
521 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
522 ResDescriptionAttribute(Res.SqlConnection_ConnectionTimeout),
524 override public int ConnectionTimeout {
526 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
527 return ((null != constr) ? constr.ConnectTimeout : SqlConnectionString.DEFAULT.Connect_Timeout);
532 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
533 ResDescriptionAttribute(Res.SqlConnection_Database),
535 override public string Database {
536 // if the connection is open, we need to ask the inner connection what it's
537 // current catalog is because it may have gotten changed, otherwise we can
538 // just return what the connection string had.
540 SqlInternalConnection innerConnection = (InnerConnection as SqlInternalConnection);
543 if (null != innerConnection) {
544 result = innerConnection.CurrentDatabase;
547 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
548 result = ((null != constr) ? constr.InitialCatalog : SqlConnectionString.DEFAULT.Initial_Catalog);
556 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
557 ResDescriptionAttribute(Res.SqlConnection_DataSource),
559 override public string DataSource {
561 SqlInternalConnection innerConnection = (InnerConnection as SqlInternalConnection);
564 if (null != innerConnection) {
565 result = innerConnection.CurrentDataSource;
568 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
569 result = ((null != constr) ? constr.DataSource : SqlConnectionString.DEFAULT.Data_Source);
576 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
577 ResCategoryAttribute(Res.DataCategory_Data),
578 ResDescriptionAttribute(Res.SqlConnection_PacketSize),
580 public int PacketSize {
581 // if the connection is open, we need to ask the inner connection what it's
582 // current packet size is because it may have gotten changed, otherwise we
583 // can just return what the connection string had.
585 if (IsContextConnection) {
586 throw SQL.NotAvailableOnContextConnection();
589 SqlInternalConnectionTds innerConnection = (InnerConnection as SqlInternalConnectionTds);
592 if (null != innerConnection) {
593 result = innerConnection.PacketSize;
596 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
597 result = ((null != constr) ? constr.PacketSize : SqlConnectionString.DEFAULT.Packet_Size);
604 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
605 ResCategoryAttribute(Res.DataCategory_Data),
606 ResDescriptionAttribute(Res.SqlConnection_ClientConnectionId),
608 public Guid ClientConnectionId {
611 SqlInternalConnectionTds innerConnection = (InnerConnection as SqlInternalConnectionTds);
613 if (null != innerConnection) {
614 return innerConnection.ClientConnectionId;
617 Task reconnectTask = _currentReconnectionTask;
618 if (reconnectTask != null && !reconnectTask.IsCompleted) {
619 return _originalConnectionId;
628 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
629 ResDescriptionAttribute(Res.SqlConnection_ServerVersion),
631 override public string ServerVersion {
633 return GetOpenConnection().ServerVersion;
639 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
640 ResDescriptionAttribute(Res.DbConnection_State),
642 override public ConnectionState State {
644 Task reconnectTask=_currentReconnectionTask;
645 if (reconnectTask != null && !reconnectTask.IsCompleted) {
646 return ConnectionState.Open;
648 return InnerConnection.State;
653 internal SqlStatistics Statistics {
660 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
661 ResCategoryAttribute(Res.DataCategory_Data),
662 ResDescriptionAttribute(Res.SqlConnection_WorkstationId),
664 public string WorkstationId {
666 if (IsContextConnection) {
667 throw SQL.NotAvailableOnContextConnection();
670 // If not supplied by the user, the default value is the MachineName
671 // Note: In Longhorn you'll be able to rename a machine without
672 // rebooting. Therefore, don't cache this machine name.
673 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
674 string result = ((null != constr) ? constr.WorkstationId : null);
675 if (null == result) {
676 // getting machine name requires Environment.Permission
677 // user must have that permission in order to retrieve this
678 result = Environment.MachineName;
684 // SqlCredential: Pair User Id and password in SecureString which are to be used for SQL authentication
687 DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
688 ResDescriptionAttribute(Res.SqlConnection_Credential),
690 public SqlCredential Credential
694 SqlCredential result = _credential;
696 // When a connection is connecting or is ever opened, make credential available only if "Persist Security Info" is set to true
697 // otherwise, return null
698 SqlConnectionString connectionOptions = (SqlConnectionString) UserConnectionOptions;
699 if (InnerConnection.ShouldHidePassword && connectionOptions != null && !connectionOptions.PersistSecurityInfo)
709 // If a connection is connecting or is ever opened, user id/password cannot be set
710 if (!InnerConnection.AllowSetConnectionString)
712 throw ADP.OpenConnectionPropertySet("Credential", InnerConnection.State);
715 // check if the usage of credential has any conflict with the keys used in connection string
718 // Check for Credential being used with Authentication=ActiveDirectoryIntegrated. Since a different error string is used
719 // for this case in ConnectionString setter vs in Credential setter, check for this error case before calling
720 // CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential, which is common to both setters.
721 if (UsesActiveDirectoryIntegrated((SqlConnectionString) ConnectionOptions)) {
722 throw SQL.SettingCredentialWithIntegratedInvalid();
725 CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential((SqlConnectionString) ConnectionOptions);
726 if(_accessToken != null) {
727 throw ADP.InvalidMixedUsageOfCredentialAndAccessToken();
734 // Need to call ConnectionString_Set to do proper pool group check
735 ConnectionString_Set(new SqlConnectionPoolKey(_connectionString, _credential, accessToken: _accessToken));
739 // CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential: check if the usage of credential has any conflict
740 // with the keys used in connection string
741 // If there is any conflict, it throws InvalidOperationException
742 // This is to be used setter of ConnectionString and Credential properties
743 private void CheckAndThrowOnInvalidCombinationOfConnectionStringAndSqlCredential(SqlConnectionString connectionOptions)
745 if (UsesClearUserIdOrPassword(connectionOptions))
747 throw ADP.InvalidMixedUsageOfSecureAndClearCredential();
750 if (UsesIntegratedSecurity(connectionOptions))
752 throw ADP.InvalidMixedUsageOfSecureCredentialAndIntegratedSecurity();
755 if (UsesContextConnection(connectionOptions))
757 throw ADP.InvalidMixedArgumentOfSecureCredentialAndContextConnection();
761 // CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessToken: check if the usage of AccessToken has any conflict
762 // with the keys used in connection string and credential
763 // If there is any conflict, it throws InvalidOperationException
764 // This is to be used setter of ConnectionString and AccessToken properties
765 private void CheckAndThrowOnInvalidCombinationOfConnectionOptionAndAccessToken(SqlConnectionString connectionOptions) {
766 if (UsesClearUserIdOrPassword(connectionOptions)) {
767 throw ADP.InvalidMixedUsageOfAccessTokenAndUserIDPassword();
770 if (UsesIntegratedSecurity(connectionOptions)) {
771 throw ADP.InvalidMixedUsageOfAccessTokenAndIntegratedSecurity();
774 if (UsesContextConnection(connectionOptions)) {
775 throw ADP.InvalidMixedUsageOfAccessTokenAndContextConnection();
778 if (UsesAuthentication(connectionOptions)) {
779 throw ADP.InvalidMixedUsageOfAccessTokenAndAuthentication();
782 // Check if the usage of AccessToken has the conflict with credential
783 if (_credential != null) {
784 throw ADP.InvalidMixedUsageOfAccessTokenAndCredential();
793 ResCategoryAttribute(Res.DataCategory_InfoMessage),
794 ResDescriptionAttribute(Res.DbConnection_InfoMessage),
796 public event SqlInfoMessageEventHandler InfoMessage {
798 Events.AddHandler(EventInfoMessage, value);
801 Events.RemoveHandler(EventInfoMessage, value);
805 public bool FireInfoMessageEventOnUserErrors {
807 return _fireInfoMessageEventOnUserErrors;
810 _fireInfoMessageEventOnUserErrors = value;
814 // Approx. number of times that the internal connection has been reconnected
815 internal int ReconnectCount {
817 return _reconnectCount;
825 new public SqlTransaction BeginTransaction() {
826 // this is just a delegate. The actual method tracks executiontime
827 return BeginTransaction(IsolationLevel.Unspecified, null);
830 new public SqlTransaction BeginTransaction(IsolationLevel iso) {
831 // this is just a delegate. The actual method tracks executiontime
832 return BeginTransaction(iso, null);
835 public SqlTransaction BeginTransaction(string transactionName) {
836 // Use transaction names only on the outermost pair of nested
837 // BEGIN...COMMIT or BEGIN...ROLLBACK statements. Transaction names
838 // are ignored for nested BEGIN's. The only way to rollback a nested
839 // transaction is to have a save point from a SAVE TRANSACTION call.
840 return BeginTransaction(IsolationLevel.Unspecified, transactionName);
843 // suppress this message - we cannot use SafeHandle here. Also, see notes in the code (VSTFDEVDIV# 560355)
844 [SuppressMessage("Microsoft.Reliability", "CA2004:RemoveCallsToGCKeepAlive")]
845 override protected DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
848 Bid.ScopeEnter(out hscp, "<prov.SqlConnection.BeginDbTransaction|API> %d#, isolationLevel=%d{ds.IsolationLevel}", ObjectID, (int)isolationLevel);
851 DbTransaction transaction = BeginTransaction(isolationLevel);
853 // VSTFDEVDIV# 560355 - InnerConnection doesn't maintain a ref on the outer connection (this) and
854 // subsequently leaves open the possibility that the outer connection could be GC'ed before the SqlTransaction
855 // is fully hooked up (leaving a DbTransaction with a null connection property). Ensure that this is reachable
856 // until the completion of BeginTransaction with KeepAlive
862 Bid.ScopeLeave(ref hscp);
866 public SqlTransaction BeginTransaction(IsolationLevel iso, string transactionName) {
867 WaitForPendingReconnection();
868 SqlStatistics statistics = null;
870 string xactName = ADP.IsEmpty(transactionName)? "None" : transactionName;
871 Bid.ScopeEnter(out hscp, "<sc.SqlConnection.BeginTransaction|API> %d#, iso=%d{ds.IsolationLevel}, transactionName='%ls'\n", ObjectID, (int)iso,
875 statistics = SqlStatistics.StartTimer(Statistics);
877 // NOTE: we used to throw an exception if the transaction name was empty
878 // (see MDAC 50292) but that was incorrect because we have a BeginTransaction
879 // method that doesn't have a transactionName argument.
880 SqlTransaction transaction;
881 bool isFirstAttempt = true;
883 transaction = GetOpenConnection().BeginSqlTransaction(iso, transactionName, isFirstAttempt); // do not reconnect twice
884 Debug.Assert(isFirstAttempt || !transaction.InternalTransaction.ConnectionHasBeenRestored, "Restored connection on non-first attempt");
885 isFirstAttempt = false;
886 } while (transaction.InternalTransaction.ConnectionHasBeenRestored);
889 // SQLBU 503873 The GetOpenConnection line above doesn't keep a ref on the outer connection (this),
890 // and it could be collected before the inner connection can hook it to the transaction, resulting in
891 // a transaction with a null connection property. Use GC.KeepAlive to ensure this doesn't happen.
897 Bid.ScopeLeave(ref hscp);
898 SqlStatistics.StopTimer(statistics);
902 override public void ChangeDatabase(string database) {
903 SqlStatistics statistics = null;
904 RepairInnerConnection();
905 Bid.CorrelationTrace("<sc.SqlConnection.ChangeDatabase|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
906 TdsParser bestEffortCleanupTarget = null;
907 RuntimeHelpers.PrepareConstrainedRegions();
910 TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
912 RuntimeHelpers.PrepareConstrainedRegions();
914 tdsReliabilitySection.Start();
918 bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(this);
919 statistics = SqlStatistics.StartTimer(Statistics);
920 InnerConnection.ChangeDatabase(database);
924 tdsReliabilitySection.Stop();
928 catch (System.OutOfMemoryException e) {
932 catch (System.StackOverflowException e) {
936 catch (System.Threading.ThreadAbortException e) {
938 SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
942 SqlStatistics.StopTimer(statistics);
946 static public void ClearAllPools() {
947 (new SqlClientPermission(PermissionState.Unrestricted)).Demand();
948 SqlConnectionFactory.SingletonInstance.ClearAllPools();
951 static public void ClearPool(SqlConnection connection) {
952 ADP.CheckArgumentNull(connection, "connection");
954 DbConnectionOptions connectionOptions = connection.UserConnectionOptions;
955 if (null != connectionOptions) {
956 connectionOptions.DemandPermission();
957 if (connection.IsContextConnection) {
958 throw SQL.NotAvailableOnContextConnection();
960 SqlConnectionFactory.SingletonInstance.ClearPool(connection);
964 object ICloneable.Clone() {
965 SqlConnection clone = new SqlConnection(this);
966 Bid.Trace("<sc.SqlConnection.Clone|API> %d#, clone=%d#\n", ObjectID, clone.ObjectID);
970 void CloseInnerConnection() {
971 // CloseConnection() now handles the lock
973 // The SqlInternalConnectionTds is set to OpenBusy during close, once this happens the cast below will fail and
974 // the command will no longer be cancelable. It might be desirable to be able to cancel the close opperation, but this is
975 // outside of the scope of Whidbey RTM. See (SqlCommand::Cancel) for other lock.
976 InnerConnection.CloseConnection(this, ConnectionFactory);
979 override public void Close() {
981 Bid.ScopeEnter(out hscp, "<sc.SqlConnection.Close|API> %d#" , ObjectID);
982 Bid.CorrelationTrace("<sc.SqlConnection.Close|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
984 SqlStatistics statistics = null;
986 TdsParser bestEffortCleanupTarget = null;
987 RuntimeHelpers.PrepareConstrainedRegions();
990 TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
992 RuntimeHelpers.PrepareConstrainedRegions();
994 tdsReliabilitySection.Start();
998 bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(this);
999 statistics = SqlStatistics.StartTimer(Statistics);
1001 Task reconnectTask = _currentReconnectionTask;
1002 if (reconnectTask != null && !reconnectTask.IsCompleted) {
1003 CancellationTokenSource cts = _reconnectionCancellationSource;
1007 AsyncHelper.WaitForCompletion(reconnectTask, 0, null, rethrowExceptions: false); // we do not need to deal with possible exceptions in reconnection
1008 if (State != ConnectionState.Open) {// if we cancelled before the connection was opened
1009 OnStateChange(DbConnectionInternal.StateChangeClosed);
1012 CancelOpenAndWait();
1013 CloseInnerConnection();
1014 GC.SuppressFinalize(this);
1016 if (null != Statistics) {
1017 ADP.TimerCurrent(out _statistics._closeTimestamp);
1022 tdsReliabilitySection.Stop();
1026 catch (System.OutOfMemoryException e) {
1030 catch (System.StackOverflowException e) {
1034 catch (System.Threading.ThreadAbortException e) {
1036 SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
1040 SqlStatistics.StopTimer(statistics);
1044 SqlDebugContext sdc = _sdc;
1046 Bid.ScopeLeave(ref hscp);
1053 new public SqlCommand CreateCommand() {
1054 return new SqlCommand(null, this);
1057 private void DisposeMe(bool disposing) { // MDAC 65459
1058 // clear credential and AccessToken here rather than in IDisposable.Dispose as these are specific to SqlConnection only
1059 // IDisposable.Dispose is generated code from a template and used by other providers as well
1061 _accessToken = null;
1068 var innerConnection = (InnerConnection as SqlInternalConnectionTds);
1069 if ((innerConnection != null) && (!innerConnection.ConnectionOptions.Pooling)) {
1070 var parser = innerConnection.Parser;
1071 if ((parser != null) && (parser._physicalStateObj != null)) {
1072 parser._physicalStateObj.DecrementPendingCallbacks(release: false);
1079 public void EnlistDistributedTransaction(System.EnterpriseServices.ITransaction transaction) {
1080 if (IsContextConnection) {
1081 throw SQL.NotAvailableOnContextConnection();
1084 EnlistDistributedTransactionHelper(transaction);
1088 override public void Open() {
1090 Bid.ScopeEnter(out hscp, "<sc.SqlConnection.Open|API> %d#", ObjectID) ;
1091 Bid.CorrelationTrace("<sc.SqlConnection.Open|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
1094 if (StatisticsEnabled) {
1095 if (null == _statistics) {
1096 _statistics = new SqlStatistics();
1099 _statistics.ContinueOnNewConnection();
1103 SqlStatistics statistics = null;
1104 RuntimeHelpers.PrepareConstrainedRegions();
1106 statistics = SqlStatistics.StartTimer(Statistics);
1108 if (!TryOpen(null)) {
1109 throw ADP.InternalError(ADP.InternalErrorCode.SynchronousConnectReturnedPending);
1113 SqlStatistics.StopTimer(statistics);
1117 Bid.ScopeLeave(ref hscp);
1121 internal void RegisterWaitingForReconnect(Task waitingTask) {
1122 if (((SqlConnectionString)ConnectionOptions).MARS) {
1125 Interlocked.CompareExchange(ref _asyncWaitingForReconnection, waitingTask, null);
1126 if (_asyncWaitingForReconnection != waitingTask) { // somebody else managed to register
1127 throw SQL.MARSUnspportedOnConnection();
1131 private async Task ReconnectAsync(int timeout) {
1133 long commandTimeoutExpiration = 0;
1135 commandTimeoutExpiration = ADP.TimerCurrent() + ADP.TimerFromSeconds(timeout);
1137 CancellationTokenSource cts = new CancellationTokenSource();
1138 _reconnectionCancellationSource = cts;
1139 CancellationToken ctoken = cts.Token;
1140 int retryCount = _connectRetryCount; // take a snapshot: could be changed by modifying the connection string
1141 for (int attempt = 0; attempt < retryCount; attempt++) {
1142 if (ctoken.IsCancellationRequested) {
1143 Bid.Trace("<sc.SqlConnection.ReconnectAsync|INFO> Orginal ClientConnectionID %ls - reconnection cancelled\n", _originalConnectionId.ToString());
1147 _impersonateIdentity = _lastIdentity;
1149 ForceNewConnection = true;
1150 await OpenAsync(ctoken).ConfigureAwait(false);
1151 // On success, increment the reconnect count - we don't really care if it rolls over since it is approx.
1152 _reconnectCount = unchecked(_reconnectCount + 1);
1154 Debug.Assert(_recoverySessionData._debugReconnectDataApplied, "Reconnect data was not applied !");
1158 _impersonateIdentity = null;
1159 ForceNewConnection = false;
1161 Bid.Trace("<sc.SqlConnection.ReconnectIfNeeded|INFO> Reconnection suceeded. ClientConnectionID %ls -> %ls \n", _originalConnectionId.ToString(), ClientConnectionId.ToString());
1164 catch (SqlException e) {
1165 Bid.Trace("<sc.SqlConnection.ReconnectAsyncINFO> Orginal ClientConnectionID %ls - reconnection attempt failed error %ls\n", _originalConnectionId.ToString(), e.Message);
1166 if (attempt == retryCount - 1) {
1167 Bid.Trace("<sc.SqlConnection.ReconnectAsync|INFO> Orginal ClientConnectionID %ls - give up reconnection\n", _originalConnectionId.ToString());
1168 throw SQL.CR_AllAttemptsFailed(e, _originalConnectionId);
1170 if (timeout > 0 && ADP.TimerRemaining(commandTimeoutExpiration) < ADP.TimerFromSeconds(ConnectRetryInterval)) {
1171 throw SQL.CR_NextAttemptWillExceedQueryTimeout(e, _originalConnectionId);
1174 await Task.Delay(1000 * ConnectRetryInterval, ctoken).ConfigureAwait(false);
1178 _recoverySessionData = null;
1179 _supressStateChangeForReconnection = false;
1181 Debug.Assert(false, "Should not reach this point");
1184 internal Task ValidateAndReconnect(Action beforeDisconnect, int timeout) {
1185 Task runningReconnect = _currentReconnectionTask;
1186 // This loop in the end will return not completed reconnect task or null
1187 while (runningReconnect != null && runningReconnect.IsCompleted) {
1188 // clean current reconnect task (if it is the same one we checked
1189 Interlocked.CompareExchange<Task>(ref _currentReconnectionTask, null, runningReconnect);
1190 // make sure nobody started new task (if which case we did not clean it)
1191 runningReconnect = _currentReconnectionTask;
1193 if (runningReconnect == null) {
1194 if (_connectRetryCount > 0) {
1195 SqlInternalConnectionTds tdsConn = GetOpenTdsConnection();
1196 if (tdsConn._sessionRecoveryAcknowledged) {
1197 TdsParserStateObject stateObj = tdsConn.Parser._physicalStateObj;
1198 if (!stateObj.ValidateSNIConnection()) {
1199 if (tdsConn.Parser._sessionPool != null) {
1200 if (tdsConn.Parser._sessionPool.ActiveSessionsCount > 0) {
1202 if (beforeDisconnect != null) {
1205 OnError(SQL.CR_UnrecoverableClient(ClientConnectionId), true, null);
1208 SessionData cData = tdsConn.CurrentSessionData;
1209 cData.AssertUnrecoverableStateCountIsCorrect();
1210 if (cData._unrecoverableStatesCount == 0) {
1211 bool callDisconnect = false;
1212 lock (_reconnectLock) {
1213 tdsConn.CheckEnlistedTransactionBinding();
1214 runningReconnect = _currentReconnectionTask; // double check after obtaining the lock
1215 if (runningReconnect == null) {
1216 if (cData._unrecoverableStatesCount == 0) { // could change since the first check, but now is stable since connection is know to be broken
1217 _originalConnectionId = ClientConnectionId;
1218 Bid.Trace("<sc.SqlConnection.ReconnectIfNeeded|INFO> Connection ClientConnectionID %ls is invalid, reconnecting\n", _originalConnectionId.ToString());
1219 _recoverySessionData = cData;
1220 if (beforeDisconnect != null) {
1224 _supressStateChangeForReconnection = true;
1225 tdsConn.DoomThisConnection();
1227 catch (SqlException) {
1229 runningReconnect = Task.Run(() => ReconnectAsync(timeout));
1230 // if current reconnect is not null, somebody already started reconnection task - some kind of race condition
1231 Debug.Assert(_currentReconnectionTask == null, "Duplicate reconnection tasks detected");
1232 _currentReconnectionTask = runningReconnect;
1236 callDisconnect = true;
1239 if (callDisconnect && beforeDisconnect != null) {
1244 if (beforeDisconnect != null) {
1247 OnError(SQL.CR_UnrecoverableServer(ClientConnectionId), true, null);
1249 } // ValidateSNIConnection
1250 } // sessionRecoverySupported
1251 } // connectRetryCount>0
1253 else { // runningReconnect = null
1254 if (beforeDisconnect != null) {
1258 return runningReconnect;
1261 // this is straightforward, but expensive method to do connection resiliency - it take locks and all prepartions as for TDS request
1262 partial void RepairInnerConnection() {
1263 WaitForPendingReconnection();
1264 if (_connectRetryCount == 0) {
1267 SqlInternalConnectionTds tdsConn = InnerConnection as SqlInternalConnectionTds;
1268 if (tdsConn != null) {
1269 tdsConn.ValidateConnectionForExecute(null);
1270 tdsConn.GetSessionAndReconnectIfNeeded((SqlConnection)this);
1274 private void WaitForPendingReconnection() {
1275 Task reconnectTask = _currentReconnectionTask;
1276 if (reconnectTask != null && !reconnectTask.IsCompleted) {
1277 AsyncHelper.WaitForCompletion(reconnectTask, 0, null, rethrowExceptions: false);
1281 void CancelOpenAndWait()
1283 // copy from member to avoid changes by background thread
1284 var completion = _currentCompletion;
1285 if (completion != null)
1287 completion.Item1.TrySetCanceled();
1288 ((IAsyncResult)completion.Item2).AsyncWaitHandle.WaitOne();
1290 Debug.Assert(_currentCompletion == null, "After waiting for an async call to complete, there should be no completion source");
1293 public override Task OpenAsync(CancellationToken cancellationToken) {
1295 Bid.ScopeEnter(out hscp, "<sc.SqlConnection.OpenAsync|API> %d#", ObjectID) ;
1296 Bid.CorrelationTrace("<sc.SqlConnection.OpenAsync|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
1299 if (StatisticsEnabled) {
1300 if (null == _statistics) {
1301 _statistics = new SqlStatistics();
1304 _statistics.ContinueOnNewConnection();
1308 SqlStatistics statistics = null;
1309 RuntimeHelpers.PrepareConstrainedRegions();
1311 statistics = SqlStatistics.StartTimer(Statistics);
1313 System.Transactions.Transaction transaction = ADP.GetCurrentTransaction();
1314 TaskCompletionSource<DbConnectionInternal> completion = new TaskCompletionSource<DbConnectionInternal>(transaction);
1315 TaskCompletionSource<object> result = new TaskCompletionSource<object>();
1317 if (cancellationToken.IsCancellationRequested) {
1318 result.SetCanceled();
1322 if (IsContextConnection) {
1323 // Async not supported on Context Connections
1324 result.SetException(ADP.ExceptionWithStackTrace(SQL.NotAvailableOnContextConnection()));
1331 completed = TryOpen(completion);
1333 catch (Exception e) {
1334 result.SetException(e);
1339 result.SetResult(null);
1342 CancellationTokenRegistration registration = new CancellationTokenRegistration();
1343 if (cancellationToken.CanBeCanceled) {
1344 registration = cancellationToken.Register(() => completion.TrySetCanceled());
1346 OpenAsyncRetry retry = new OpenAsyncRetry(this, completion, result, registration);
1347 _currentCompletion = new Tuple<TaskCompletionSource<DbConnectionInternal>, Task>(completion, result.Task);
1348 completion.Task.ContinueWith(retry.Retry, TaskScheduler.Default);
1355 SqlStatistics.StopTimer(statistics);
1359 Bid.ScopeLeave(ref hscp);
1363 private class OpenAsyncRetry {
1364 SqlConnection _parent;
1365 TaskCompletionSource<DbConnectionInternal> _retry;
1366 TaskCompletionSource<object> _result;
1367 CancellationTokenRegistration _registration;
1369 public OpenAsyncRetry(SqlConnection parent, TaskCompletionSource<DbConnectionInternal> retry, TaskCompletionSource<object> result, CancellationTokenRegistration registration) {
1373 _registration = registration;
1376 internal void Retry(Task<DbConnectionInternal> retryTask) {
1377 Bid.Trace("<sc.SqlConnection.OpenAsyncRetry|Info> %d#\n", _parent.ObjectID);
1378 _registration.Dispose();
1380 SqlStatistics statistics = null;
1381 RuntimeHelpers.PrepareConstrainedRegions();
1383 statistics = SqlStatistics.StartTimer(_parent.Statistics);
1385 if (retryTask.IsFaulted) {
1386 Exception e = retryTask.Exception.InnerException;
1387 _parent.CloseInnerConnection();
1388 _parent._currentCompletion = null;
1389 _result.SetException(retryTask.Exception.InnerException);
1391 else if (retryTask.IsCanceled) {
1392 _parent.CloseInnerConnection();
1393 _parent._currentCompletion = null;
1394 _result.SetCanceled();
1398 // protect continuation from ----s with close and cancel
1399 lock (_parent.InnerConnection) {
1400 result = _parent.TryOpen(_retry);
1404 _parent._currentCompletion = null;
1405 _result.SetResult(null);
1408 _parent.CloseInnerConnection();
1409 _parent._currentCompletion = null;
1410 _result.SetException(ADP.ExceptionWithStackTrace(ADP.InternalError(ADP.InternalErrorCode.CompletedConnectReturnedPending)));
1415 SqlStatistics.StopTimer(statistics);
1418 catch (Exception e) {
1419 _parent.CloseInnerConnection();
1420 _parent._currentCompletion = null;
1421 _result.SetException(e);
1426 private bool TryOpen(TaskCompletionSource<DbConnectionInternal> retry) {
1427 SqlConnectionString connectionOptions = (SqlConnectionString)ConnectionOptions;
1429 if (connectionOptions != null &&
1430 (connectionOptions.Authentication == SqlAuthenticationMethod.SqlPassword || connectionOptions.Authentication == SqlAuthenticationMethod.ActiveDirectoryPassword) &&
1431 (!connectionOptions.HasUserIdKeyword || !connectionOptions.HasPasswordKeyword) &&
1432 _credential == null) {
1433 throw SQL.CredentialsNotProvided(connectionOptions.Authentication);
1436 if (_impersonateIdentity != null) {
1437 if (_impersonateIdentity.User == DbConnectionPoolIdentity.GetCurrentWindowsIdentity().User) {
1438 return TryOpenInner(retry);
1441 using (WindowsImpersonationContext context = _impersonateIdentity.Impersonate()) {
1442 return TryOpenInner(retry);
1447 if (this.UsesIntegratedSecurity(connectionOptions) || this.UsesActiveDirectoryIntegrated(connectionOptions)) {
1448 _lastIdentity = DbConnectionPoolIdentity.GetCurrentWindowsIdentity();
1451 _lastIdentity = null;
1453 return TryOpenInner(retry);
1457 private bool TryOpenInner(TaskCompletionSource<DbConnectionInternal> retry) {
1458 TdsParser bestEffortCleanupTarget = null;
1459 RuntimeHelpers.PrepareConstrainedRegions();
1462 TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
1464 RuntimeHelpers.PrepareConstrainedRegions();
1466 tdsReliabilitySection.Start();
1470 if (ForceNewConnection) {
1471 if (!InnerConnection.TryReplaceConnection(this, ConnectionFactory, retry, UserConnectionOptions)) {
1476 if (!InnerConnection.TryOpenConnection(this, ConnectionFactory, retry, UserConnectionOptions)) {
1480 // does not require GC.KeepAlive(this) because of OnStateChange
1482 // GetBestEffortCleanup must happen AFTER OpenConnection to get the correct target.
1483 bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(this);
1485 var tdsInnerConnection = (InnerConnection as SqlInternalConnectionTds);
1486 if (tdsInnerConnection == null) {
1487 SqlInternalConnectionSmi innerConnection = (InnerConnection as SqlInternalConnectionSmi);
1488 innerConnection.AutomaticEnlistment();
1491 Debug.Assert(tdsInnerConnection.Parser != null, "Where's the parser?");
1493 if (!tdsInnerConnection.ConnectionOptions.Pooling) {
1494 // For non-pooled connections, we need to make sure that the finalizer does actually run to avoid leaking SNI handles
1495 GC.ReRegisterForFinalize(this);
1498 if (StatisticsEnabled) {
1499 ADP.TimerCurrent(out _statistics._openTimestamp);
1500 tdsInnerConnection.Parser.Statistics = _statistics;
1503 tdsInnerConnection.Parser.Statistics = null;
1504 _statistics = null; // in case of previous Open/Close/reset_CollectStats sequence
1511 tdsReliabilitySection.Stop();
1515 catch (System.OutOfMemoryException e) {
1519 catch (System.StackOverflowException e) {
1523 catch (System.Threading.ThreadAbortException e) {
1525 SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
1534 // INTERNAL PROPERTIES
1537 internal bool HasLocalTransaction {
1539 return GetOpenConnection().HasLocalTransaction;
1543 internal bool HasLocalTransactionFromAPI {
1545 Task reconnectTask = _currentReconnectionTask;
1546 if (reconnectTask != null && !reconnectTask.IsCompleted) {
1547 return false; //we will not go into reconnection if we are inside the transaction
1549 return GetOpenConnection().HasLocalTransactionFromAPI;
1553 internal bool IsShiloh {
1555 if (_currentReconnectionTask != null) { // holds true even if task is completed
1556 return true; // if CR is enabled, connection, if established, will be Katmai+
1558 return GetOpenConnection().IsShiloh;
1562 internal bool IsYukonOrNewer {
1564 if (_currentReconnectionTask != null) { // holds true even if task is completed
1565 return true; // if CR is enabled, connection, if established, will be Katmai+
1567 return GetOpenConnection().IsYukonOrNewer;
1571 internal bool IsKatmaiOrNewer {
1573 if (_currentReconnectionTask != null) { // holds true even if task is completed
1574 return true; // if CR is enabled, connection, if established, will be Katmai+
1576 return GetOpenConnection().IsKatmaiOrNewer;
1580 internal TdsParser Parser {
1582 SqlInternalConnectionTds tdsConnection = (GetOpenConnection() as SqlInternalConnectionTds);
1583 if (null == tdsConnection) {
1584 throw SQL.NotAvailableOnContextConnection();
1586 return tdsConnection.Parser;
1590 internal bool Asynchronous {
1592 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
1593 return ((null != constr) ? constr.Asynchronous : SqlConnectionString.DEFAULT.Asynchronous);
1601 internal void ValidateConnectionForExecute(string method, SqlCommand command) {
1602 Task asyncWaitingForReconnection=_asyncWaitingForReconnection;
1603 if (asyncWaitingForReconnection!=null) {
1604 if (!asyncWaitingForReconnection.IsCompleted) {
1605 throw SQL.MARSUnspportedOnConnection();
1608 Interlocked.CompareExchange(ref _asyncWaitingForReconnection, null, asyncWaitingForReconnection);
1611 if (_currentReconnectionTask != null) {
1612 Task currentReconnectionTask = _currentReconnectionTask;
1613 if (currentReconnectionTask != null && !currentReconnectionTask.IsCompleted) {
1614 return; // execution will wait for this task later
1617 SqlInternalConnection innerConnection = GetOpenConnection(method);
1618 innerConnection.ValidateConnectionForExecute(command);
1621 // Surround name in brackets and then escape any end bracket to protect against SQL Injection.
1622 // NOTE: if the user escapes it themselves it will not work, but this was the case in V1 as well
1623 // as native OleDb and Odbc.
1624 static internal string FixupDatabaseTransactionName(string name) {
1625 if (!ADP.IsEmpty(name)) {
1626 return SqlServerEscapeHelper.EscapeIdentifier(name);
1633 // If wrapCloseInAction is defined, then the action it defines will be run with the connection close action passed in as a parameter
1634 // The close action also supports being run asynchronously
1635 internal void OnError(SqlException exception, bool breakConnection, Action<Action> wrapCloseInAction) {
1636 Debug.Assert(exception != null && exception.Errors.Count != 0, "SqlConnection: OnError called with null or empty exception!");
1646 if (breakConnection && (ConnectionState.Open == State)) {
1648 if (wrapCloseInAction != null) {
1649 int capturedCloseCount = _closeCount;
1651 Action closeAction = () => {
1652 if (capturedCloseCount == _closeCount) {
1653 Bid.Trace("<sc.SqlConnection.OnError|INFO> %d#, Connection broken.\n", ObjectID);
1658 wrapCloseInAction(closeAction);
1661 Bid.Trace("<sc.SqlConnection.OnError|INFO> %d#, Connection broken.\n", ObjectID);
1666 if (exception.Class >= TdsEnums.MIN_ERROR_CLASS) {
1667 // It is an error, and should be thrown. Class of TdsEnums.MIN_ERROR_CLASS or above is an error,
1668 // below TdsEnums.MIN_ERROR_CLASS denotes an info message.
1672 // If it is a class < TdsEnums.MIN_ERROR_CLASS, it is a warning collection - so pass to handler
1673 this.OnInfoMessage(new SqlInfoMessageEventArgs(exception));
1681 // SxS: using Debugger.IsAttached
1683 [ResourceExposure(ResourceScope.None)]
1684 [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
1685 private void CompleteOpen() {
1686 Debug.Assert(ConnectionState.Open == State, "CompleteOpen not open");
1687 // be sure to mark as open so SqlDebugCheck can issue Query
1689 // check to see if we need to hook up sql-debugging if a debugger is attached
1690 // We only need this check for Shiloh and earlier servers.
1691 if (!GetOpenConnection().IsYukonOrNewer &&
1692 System.Diagnostics.Debugger.IsAttached) {
1693 bool debugCheck = false;
1695 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand(); // MDAC 66682, 69017
1698 catch (SecurityException e) {
1699 ADP.TraceExceptionWithoutRethrow(e);
1703 // if we don't have Unmanaged code permission, don't check for debugging
1704 // but let the connection be opened while under the debugger
1705 CheckSQLDebugOnConnect();
1710 internal SqlInternalConnection GetOpenConnection() {
1711 SqlInternalConnection innerConnection = (InnerConnection as SqlInternalConnection);
1712 if (null == innerConnection) {
1713 throw ADP.ClosedConnectionError();
1715 return innerConnection;
1718 internal SqlInternalConnection GetOpenConnection(string method) {
1719 DbConnectionInternal innerConnection = InnerConnection;
1720 SqlInternalConnection innerSqlConnection = (innerConnection as SqlInternalConnection);
1721 if (null == innerSqlConnection) {
1722 throw ADP.OpenConnectionRequired(method, innerConnection.State);
1724 return innerSqlConnection;
1727 internal SqlInternalConnectionTds GetOpenTdsConnection() {
1728 SqlInternalConnectionTds innerConnection = (InnerConnection as SqlInternalConnectionTds);
1729 if (null == innerConnection) {
1730 throw ADP.ClosedConnectionError();
1732 return innerConnection;
1735 internal SqlInternalConnectionTds GetOpenTdsConnection(string method) {
1736 SqlInternalConnectionTds innerConnection = (InnerConnection as SqlInternalConnectionTds);
1737 if (null == innerConnection) {
1738 throw ADP.OpenConnectionRequired(method, InnerConnection.State);
1740 return innerConnection;
1743 internal void OnInfoMessage(SqlInfoMessageEventArgs imevent) {
1745 OnInfoMessage(imevent, out notified);
1748 internal void OnInfoMessage(SqlInfoMessageEventArgs imevent, out bool notified) {
1750 Debug.Assert(null != imevent, "null SqlInfoMessageEventArgs");
1751 Bid.Trace("<sc.SqlConnection.OnInfoMessage|API|INFO> %d#, Message='%ls'\n", ObjectID, ((null != imevent) ? imevent.Message : ""));
1753 SqlInfoMessageEventHandler handler = (SqlInfoMessageEventHandler)Events[EventInfoMessage];
1754 if (null != handler) {
1757 handler(this, imevent);
1759 catch (Exception e) { // MDAC 53175
1760 if (!ADP.IsCatchableOrSecurityExceptionType(e)) {
1764 ADP.TraceExceptionWithoutRethrow(e);
1772 // SQL DEBUGGING SUPPORT
1775 // this only happens once per connection
1776 // SxS: using named file mapping APIs
1778 [ResourceExposure(ResourceScope.None)]
1779 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1780 private void CheckSQLDebugOnConnect() {
1782 uint pid = (uint)SafeNativeMethods.GetCurrentProcessId();
1786 // If Win2k or later, prepend "Global\\" to enable this to work through TerminalServices.
1787 if (ADP.IsPlatformNT5) {
1788 mapFileName = "Global\\" + TdsEnums.SDCI_MAPFILENAME;
1791 mapFileName = TdsEnums.SDCI_MAPFILENAME;
1794 mapFileName = mapFileName + pid.ToString(CultureInfo.InvariantCulture);
1796 hFileMap = NativeMethods.OpenFileMappingA(0x4/*FILE_MAP_READ*/, false, mapFileName);
1798 if (ADP.PtrZero != hFileMap) {
1799 IntPtr pMemMap = NativeMethods.MapViewOfFile(hFileMap, 0x4/*FILE_MAP_READ*/, 0, 0, IntPtr.Zero);
1800 if (ADP.PtrZero != pMemMap) {
1801 SqlDebugContext sdc = new SqlDebugContext();
1802 sdc.hMemMap = hFileMap;
1803 sdc.pMemMap = pMemMap;
1806 // optimization: if we only have to refresh memory-mapped data at connection open time
1807 // optimization: then call here instead of in CheckSQLDebug() which gets called
1808 // optimization: at command execution time
1809 // RefreshMemoryMappedData(sdc);
1811 // delaying setting out global state until after we issue this first SQLDebug command so that
1812 // we don't reentrantly call into CheckSQLDebug
1814 // now set our global state
1820 // This overload is called by the Command object when executing stored procedures. Note that
1821 // if SQLDebug has never been called, it is a noop.
1822 internal void CheckSQLDebug() {
1824 CheckSQLDebug(_sdc);
1827 // SxS: using GetCurrentThreadId
1828 [ResourceExposure(ResourceScope.None)]
1829 [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
1830 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] // MDAC 66682, 69017
1831 private void CheckSQLDebug(SqlDebugContext sdc) {
1832 // check to see if debugging has been activated
1833 Debug.Assert(null != sdc, "SQL Debug: invalid null debugging context!");
1835 #pragma warning disable 618
1836 uint tid = (uint)AppDomain.GetCurrentThreadId(); // Sql Debugging doesn't need fiber support;
1837 #pragma warning restore 618
1838 RefreshMemoryMappedData(sdc);
1844 // If we get here, the debugger must be hooked up.
1846 if (sdc.fOption/*TdsEnums.SQLDEBUG_ON*/) {
1851 IssueSQLDebug(TdsEnums.SQLDEBUG_ON, sdc.machineName, sdc.pid, sdc.dbgpid, sdc.sdiDllName, sdc.data);
1852 sdc.tid = 0; // reset so that the first successful time through, we notify the server of the context switch
1861 // be sure to pick up thread context switch, especially the first time through
1863 if (!sdc.fOption/*TdsEnums.SQLDEBUG_OFF*/) {
1864 // turn off and free the memory
1866 // okay if we throw out here, no state to clean up
1867 IssueSQLDebug(TdsEnums.SQLDEBUG_OFF, null, 0, 0, null, null);
1870 // notify server of context change
1871 if (sdc.tid != tid) {
1874 IssueSQLDebug(TdsEnums.SQLDEBUG_CONTEXT, null, sdc.pid, sdc.tid, null, null);
1885 private void IssueSQLDebug(uint option, string machineName, uint pid, uint id, string sdiDllName, byte[] data) {
1887 if (GetOpenConnection().IsYukonOrNewer) {
1894 SqlCommand c = new SqlCommand(TdsEnums.SP_SDIDEBUG, this);
1895 c.CommandType = CommandType.StoredProcedure;
1898 SqlParameter p = new SqlParameter(null, SqlDbType.VarChar, TdsEnums.SQLDEBUG_MODE_NAMES[option].Length);
1899 p.Value = TdsEnums.SQLDEBUG_MODE_NAMES[option];
1900 c.Parameters.Add(p);
1902 if (option == TdsEnums.SQLDEBUG_ON) {
1904 p = new SqlParameter(null, SqlDbType.VarChar, sdiDllName.Length);
1905 p.Value = sdiDllName;
1906 c.Parameters.Add(p);
1907 // debug machine name
1908 p = new SqlParameter(null, SqlDbType.VarChar, machineName.Length);
1909 p.Value = machineName;
1910 c.Parameters.Add(p);
1913 if (option != TdsEnums.SQLDEBUG_OFF) {
1915 p = new SqlParameter(null, SqlDbType.Int);
1917 c.Parameters.Add(p);
1919 p = new SqlParameter(null, SqlDbType.Int);
1921 c.Parameters.Add(p);
1924 if (option == TdsEnums.SQLDEBUG_ON) {
1926 p = new SqlParameter(null, SqlDbType.VarBinary, (null != data) ? data.Length : 0);
1928 c.Parameters.Add(p);
1931 c.ExecuteNonQuery();
1935 public static void ChangePassword(string connectionString, string newPassword) {
1937 Bid.ScopeEnter(out hscp, "<sc.SqlConnection.ChangePassword|API>") ;
1938 Bid.CorrelationTrace("<sc.SqlConnection.ChangePassword|API|Correlation> ActivityID %ls\n");
1940 if (ADP.IsEmpty(connectionString)) {
1941 throw SQL.ChangePasswordArgumentMissing("connectionString");
1943 if (ADP.IsEmpty(newPassword)) {
1944 throw SQL.ChangePasswordArgumentMissing("newPassword");
1946 if (TdsEnums.MAXLEN_NEWPASSWORD < newPassword.Length) {
1947 throw ADP.InvalidArgumentLength("newPassword", TdsEnums.MAXLEN_NEWPASSWORD);
1950 SqlConnectionPoolKey key = new SqlConnectionPoolKey(connectionString, credential: null, accessToken: null);
1952 SqlConnectionString connectionOptions = SqlConnectionFactory.FindSqlConnectionOptions(key);
1953 if (connectionOptions.IntegratedSecurity || connectionOptions.Authentication == SqlAuthenticationMethod.ActiveDirectoryIntegrated) {
1954 throw SQL.ChangePasswordConflictsWithSSPI();
1956 if (! ADP.IsEmpty(connectionOptions.AttachDBFilename)) {
1957 throw SQL.ChangePasswordUseOfUnallowedKey(SqlConnectionString.KEY.AttachDBFilename);
1959 if (connectionOptions.ContextConnection) {
1960 throw SQL.ChangePasswordUseOfUnallowedKey(SqlConnectionString.KEY.Context_Connection);
1963 System.Security.PermissionSet permissionSet = connectionOptions.CreatePermissionSet();
1964 permissionSet.Demand();
1966 ChangePassword(connectionString, connectionOptions, null, newPassword, null);
1969 Bid.ScopeLeave(ref hscp) ;
1973 public static void ChangePassword(string connectionString, SqlCredential credential, SecureString newSecurePassword) {
1975 Bid.ScopeEnter(out hscp, "<sc.SqlConnection.ChangePassword|API>") ;
1976 Bid.CorrelationTrace("<sc.SqlConnection.ChangePassword|API|Correlation> ActivityID %ls\n");
1978 if (ADP.IsEmpty(connectionString)) {
1979 throw SQL.ChangePasswordArgumentMissing("connectionString");
1982 // check credential; not necessary to check the length of password in credential as the check is done by SqlCredential class
1983 if (credential == null) {
1984 throw SQL.ChangePasswordArgumentMissing("credential");
1987 if (newSecurePassword == null || newSecurePassword.Length == 0) {
1988 throw SQL.ChangePasswordArgumentMissing("newSecurePassword");;
1991 if (!newSecurePassword.IsReadOnly()) {
1992 throw ADP.MustBeReadOnly("newSecurePassword");
1995 if (TdsEnums.MAXLEN_NEWPASSWORD < newSecurePassword.Length) {
1996 throw ADP.InvalidArgumentLength("newSecurePassword", TdsEnums.MAXLEN_NEWPASSWORD);
1999 SqlConnectionPoolKey key = new SqlConnectionPoolKey(connectionString, credential, accessToken: null);
2001 SqlConnectionString connectionOptions = SqlConnectionFactory.FindSqlConnectionOptions(key);
2003 // Check for incompatible connection string value with SqlCredential
2004 if (!ADP.IsEmpty(connectionOptions.UserID) || !ADP.IsEmpty(connectionOptions.Password)) {
2005 throw ADP.InvalidMixedArgumentOfSecureAndClearCredential();
2008 if (connectionOptions.IntegratedSecurity || connectionOptions.Authentication == SqlAuthenticationMethod.ActiveDirectoryIntegrated) {
2009 throw SQL.ChangePasswordConflictsWithSSPI();
2012 if (! ADP.IsEmpty(connectionOptions.AttachDBFilename)) {
2013 throw SQL.ChangePasswordUseOfUnallowedKey(SqlConnectionString.KEY.AttachDBFilename);
2016 if (connectionOptions.ContextConnection) {
2017 throw SQL.ChangePasswordUseOfUnallowedKey(SqlConnectionString.KEY.Context_Connection);
2020 System.Security.PermissionSet permissionSet = connectionOptions.CreatePermissionSet();
2021 permissionSet.Demand();
2023 ChangePassword(connectionString, connectionOptions, credential, null, newSecurePassword);
2026 Bid.ScopeLeave(ref hscp) ;
2030 private static void ChangePassword(string connectionString, SqlConnectionString connectionOptions, SqlCredential credential, string newPassword, SecureString newSecurePassword ) {
2031 // note: This is the only case where we directly construt the internal connection, passing in the new password.
2032 // Normally we would simply create a regular connectoin and open it but there is no other way to pass the
2033 // new password down to the constructor. Also it would have an unwanted impact on the connection pool
2035 using (SqlInternalConnectionTds con = new SqlInternalConnectionTds(null, connectionOptions, credential, null, newPassword, newSecurePassword, false)) {
2036 if (!con.IsYukonOrNewer) {
2037 throw SQL.ChangePasswordRequiresYukon();
2040 SqlConnectionPoolKey key = new SqlConnectionPoolKey(connectionString, credential, accessToken: null);
2042 SqlConnectionFactory.SingletonInstance.ClearPool(key);
2045 internal void RegisterForConnectionCloseNotification<T>(ref Task<T> outterTask, object value, int tag) {
2046 // Connection exists, schedule removal, will be added to ref collection after calling ValidateAndReconnect
2047 outterTask = outterTask.ContinueWith(task => {
2048 RemoveWeakReference(value);
2050 }, TaskScheduler.Default).Unwrap();
2053 // updates our context with any changes made to the memory-mapped data by an external process
2054 static private void RefreshMemoryMappedData(SqlDebugContext sdc) {
2055 Debug.Assert(ADP.PtrZero != sdc.pMemMap, "SQL Debug: invalid null value for pMemMap!");
2056 // copy memory mapped file contents into managed types
2057 MEMMAP memMap = (MEMMAP)Marshal.PtrToStructure(sdc.pMemMap, typeof(MEMMAP));
2058 sdc.dbgpid = memMap.dbgpid;
2059 sdc.fOption = (memMap.fOption == 1) ? true : false;
2060 // xlate ansi byte[] -> managed strings
2061 Encoding cp = System.Text.Encoding.GetEncoding(TdsEnums.DEFAULT_ENGLISH_CODE_PAGE_VALUE);
2062 sdc.machineName = cp.GetString(memMap.rgbMachineName, 0, memMap.rgbMachineName.Length);
2063 sdc.sdiDllName = cp.GetString(memMap.rgbDllName, 0, memMap.rgbDllName.Length);
2064 // just get data reference
2065 sdc.data = memMap.rgbData;
2068 public void ResetStatistics() {
2069 if (IsContextConnection) {
2070 throw SQL.NotAvailableOnContextConnection();
2073 if (null != Statistics) {
2075 if (ConnectionState.Open == State) {
2076 // update timestamp;
2077 ADP.TimerCurrent(out _statistics._openTimestamp);
2082 public IDictionary RetrieveStatistics() {
2083 if (IsContextConnection) {
2084 throw SQL.NotAvailableOnContextConnection();
2087 if (null != Statistics) {
2089 return Statistics.GetHashtable();
2092 return new SqlStatistics().GetHashtable();
2096 private void UpdateStatistics() {
2097 if (ConnectionState.Open == State) {
2099 ADP.TimerCurrent(out _statistics._closeTimestamp);
2101 // delegate the rest of the work to the SqlStatistics class
2102 Statistics.UpdateStatistics();
2109 private Assembly ResolveTypeAssembly(AssemblyName asmRef, bool throwOnError) {
2110 Debug.Assert(TypeSystemAssemblyVersion != null, "TypeSystemAssembly should be set !");
2111 if (string.Compare(asmRef.Name, "Microsoft.SqlServer.Types", StringComparison.OrdinalIgnoreCase) == 0) {
2113 if (asmRef.Version!=TypeSystemAssemblyVersion) {
2114 Bid.Trace("<sc.SqlConnection.ResolveTypeAssembly> SQL CLR type version change: Server sent %ls, client will instantiate %ls",
2115 asmRef.Version.ToString(), TypeSystemAssemblyVersion.ToString());
2118 asmRef.Version = TypeSystemAssemblyVersion;
2121 return Assembly.Load(asmRef);
2123 catch (Exception e) {
2124 if (throwOnError || !ADP.IsCatchableExceptionType(e)) {
2134 internal void CheckGetExtendedUDTInfo(SqlMetaDataPriv metaData, bool fThrow) {
2135 if (metaData.udtType == null) { // If null, we have not obtained extended info.
2136 Debug.Assert(!ADP.IsEmpty(metaData.udtAssemblyQualifiedName), "Unexpected state on GetUDTInfo");
2137 // Parameter throwOnError determines whether exception from Assembly.Load is thrown.
2139 Type.GetType(typeName:metaData.udtAssemblyQualifiedName, assemblyResolver:asmRef => ResolveTypeAssembly(asmRef, fThrow), typeResolver:null, throwOnError: fThrow);
2141 if (fThrow && metaData.udtType == null) {
2143 throw SQL.UDTUnexpectedResult(metaData.udtAssemblyQualifiedName);
2148 internal object GetUdtValue(object value, SqlMetaDataPriv metaData, bool returnDBNull) {
2149 if (returnDBNull && ADP.IsNull(value)) {
2150 return DBNull.Value;
2155 // Since the serializer doesn't handle nulls...
2156 if (ADP.IsNull(value)) {
2157 Type t = metaData.udtType;
2158 Debug.Assert(t != null, "Unexpected null of udtType on GetUdtValue!");
2159 o = t.InvokeMember("Null", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static, null, null, new Object[]{}, CultureInfo.InvariantCulture);
2160 Debug.Assert(o != null);
2165 MemoryStream stm = new MemoryStream((byte[]) value);
2167 o = SerializationHelperSql9.Deserialize(stm, metaData.udtType);
2169 Debug.Assert(o != null, "object could NOT be created");
2174 internal byte[] GetBytes(object o) {
2175 Microsoft.SqlServer.Server.Format format = Microsoft.SqlServer.Server.Format.Native;
2177 return GetBytes(o, out format, out maxSize);
2180 internal byte[] GetBytes(object o, out Microsoft.SqlServer.Server.Format format, out int maxSize) {
2181 SqlUdtInfo attr = AssemblyCache.GetInfoFromType(o.GetType());
2182 maxSize = attr.MaxByteSize;
2183 format = attr.SerializationFormat;
2185 if (maxSize < -1 || maxSize >= UInt16.MaxValue) { // Do we need this? Is this the right place?
2186 throw new InvalidOperationException(o.GetType() + ": invalid Size");
2191 using (MemoryStream stm = new MemoryStream(maxSize < 0 ? 0 : maxSize)) {
2192 SerializationHelperSql9.Serialize(stm, o);
2193 retval = stm.ToArray();
2207 ClassInterface(ClassInterfaceType.None),
2208 Guid("afef65ad-4577-447a-a148-83acadd3d4b9"),
2210 [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
2211 public sealed class SQLDebugging: ISQLDebug {
2214 const int STANDARD_RIGHTS_REQUIRED = (0x000F0000);
2215 const int DELETE = (0x00010000);
2216 const int READ_CONTROL = (0x00020000);
2217 const int WRITE_DAC = (0x00040000);
2218 const int WRITE_OWNER = (0x00080000);
2219 const int SYNCHRONIZE = (0x00100000);
2220 const int FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x000001FF);
2221 const uint GENERIC_READ = (0x80000000);
2222 const uint GENERIC_WRITE = (0x40000000);
2223 const uint GENERIC_EXECUTE = (0x20000000);
2224 const uint GENERIC_ALL = (0x10000000);
2226 const int SECURITY_DESCRIPTOR_REVISION = (1);
2227 const int ACL_REVISION = (2);
2229 const int SECURITY_AUTHENTICATED_USER_RID = (0x0000000B);
2230 const int SECURITY_LOCAL_SYSTEM_RID = (0x00000012);
2231 const int SECURITY_BUILTIN_DOMAIN_RID = (0x00000020);
2232 const int SECURITY_WORLD_RID = (0x00000000);
2233 const byte SECURITY_NT_AUTHORITY = 5;
2234 const int DOMAIN_GROUP_RID_ADMINS = (0x00000200);
2235 const int DOMAIN_ALIAS_RID_ADMINS = (0x00000220);
2237 const int sizeofSECURITY_ATTRIBUTES = 12; // sizeof(SECURITY_ATTRIBUTES);
2238 const int sizeofSECURITY_DESCRIPTOR = 20; // sizeof(SECURITY_DESCRIPTOR);
2239 const int sizeofACCESS_ALLOWED_ACE = 12; // sizeof(ACCESS_ALLOWED_ACE);
2240 const int sizeofACCESS_DENIED_ACE = 12; // sizeof(ACCESS_DENIED_ACE);
2241 const int sizeofSID_IDENTIFIER_AUTHORITY = 6; // sizeof(SID_IDENTIFIER_AUTHORITY)
2242 const int sizeofACL = 8; // sizeof(ACL);
2244 private IntPtr CreateSD(ref IntPtr pDacl) {
2245 IntPtr pSecurityDescriptor = IntPtr.Zero;
2246 IntPtr pUserSid = IntPtr.Zero;
2247 IntPtr pAdminSid = IntPtr.Zero;
2248 IntPtr pNtAuthority = IntPtr.Zero;
2250 bool status = false;
2252 pNtAuthority = Marshal.AllocHGlobal(sizeofSID_IDENTIFIER_AUTHORITY);
2253 if (pNtAuthority == IntPtr.Zero)
2255 Marshal.WriteInt32(pNtAuthority, 0, 0);
2256 Marshal.WriteByte(pNtAuthority, 4, 0);
2257 Marshal.WriteByte(pNtAuthority, 5, SECURITY_NT_AUTHORITY);
2260 NativeMethods.AllocateAndInitializeSid(
2263 SECURITY_AUTHENTICATED_USER_RID,
2273 if (!status || pUserSid == IntPtr.Zero) {
2277 NativeMethods.AllocateAndInitializeSid(
2280 SECURITY_BUILTIN_DOMAIN_RID,
2281 DOMAIN_ALIAS_RID_ADMINS,
2290 if (!status || pAdminSid == IntPtr.Zero) {
2294 pSecurityDescriptor = Marshal.AllocHGlobal(sizeofSECURITY_DESCRIPTOR);
2295 if (pSecurityDescriptor == IntPtr.Zero) {
2298 for (int i = 0; i < sizeofSECURITY_DESCRIPTOR; i++)
2299 Marshal.WriteByte(pSecurityDescriptor, i, (byte)0);
2301 + (2 * (sizeofACCESS_ALLOWED_ACE))
2302 + sizeofACCESS_DENIED_ACE
2303 + NativeMethods.GetLengthSid(pUserSid)
2304 + NativeMethods.GetLengthSid(pAdminSid);
2306 pDacl = Marshal.AllocHGlobal(cbAcl);
2307 if (pDacl == IntPtr.Zero) {
2310 // rights must be added in a certain order. Namely, deny access first, then add access
2311 if (NativeMethods.InitializeAcl(pDacl, cbAcl, ACL_REVISION))
2312 if (NativeMethods.AddAccessDeniedAce(pDacl, ACL_REVISION, WRITE_DAC, pUserSid))
2313 if (NativeMethods.AddAccessAllowedAce(pDacl, ACL_REVISION, GENERIC_READ, pUserSid))
2314 if (NativeMethods.AddAccessAllowedAce(pDacl, ACL_REVISION, GENERIC_ALL, pAdminSid))
2315 if (NativeMethods.InitializeSecurityDescriptor(pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION))
2316 if (NativeMethods.SetSecurityDescriptorDacl(pSecurityDescriptor, true, pDacl, false)) {
2321 if (pNtAuthority != IntPtr.Zero) {
2322 Marshal.FreeHGlobal(pNtAuthority);
2324 if (pAdminSid != IntPtr.Zero)
2325 NativeMethods.FreeSid(pAdminSid);
2326 if (pUserSid != IntPtr.Zero)
2327 NativeMethods.FreeSid(pUserSid);
2329 return pSecurityDescriptor;
2331 if (pSecurityDescriptor != IntPtr.Zero) {
2332 Marshal.FreeHGlobal(pSecurityDescriptor);
2338 // SxS: using file mapping API (CreateFileMapping)
2340 [ResourceExposure(ResourceScope.None)]
2341 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
2342 bool ISQLDebug.SQLDebug(int dwpidDebugger, int dwpidDebuggee, [MarshalAs(UnmanagedType.LPStr)] string pszMachineName,
2343 [MarshalAs(UnmanagedType.LPStr)] string pszSDIDLLName, int dwOption, int cbData, byte[] rgbData) {
2344 bool result = false;
2345 IntPtr hFileMap = IntPtr.Zero;
2346 IntPtr pMemMap = IntPtr.Zero;
2347 IntPtr pSecurityDescriptor = IntPtr.Zero;
2348 IntPtr pSecurityAttributes = IntPtr.Zero;
2349 IntPtr pDacl = IntPtr.Zero;
2351 // validate the structure
2352 if (null == pszMachineName || null == pszSDIDLLName)
2355 if (pszMachineName.Length > TdsEnums.SDCI_MAX_MACHINENAME ||
2356 pszSDIDLLName.Length > TdsEnums.SDCI_MAX_DLLNAME)
2359 // note that these are ansi strings
2360 Encoding cp = System.Text.Encoding.GetEncoding(TdsEnums.DEFAULT_ENGLISH_CODE_PAGE_VALUE);
2361 byte[] rgbMachineName = cp.GetBytes(pszMachineName);
2362 byte[] rgbSDIDLLName = cp.GetBytes(pszSDIDLLName);
2364 if (null != rgbData && cbData > TdsEnums.SDCI_MAX_DATA)
2369 // If Win2k or later, prepend "Global\\" to enable this to work through TerminalServices.
2370 if (ADP.IsPlatformNT5) {
2371 mapFileName = "Global\\" + TdsEnums.SDCI_MAPFILENAME;
2374 mapFileName = TdsEnums.SDCI_MAPFILENAME;
2377 mapFileName = mapFileName + dwpidDebuggee.ToString(CultureInfo.InvariantCulture);
2379 // Create Security Descriptor
2380 pSecurityDescriptor = CreateSD(ref pDacl);
2381 pSecurityAttributes = Marshal.AllocHGlobal(sizeofSECURITY_ATTRIBUTES);
2382 if ((pSecurityDescriptor == IntPtr.Zero) || (pSecurityAttributes == IntPtr.Zero))
2385 Marshal.WriteInt32(pSecurityAttributes, 0, sizeofSECURITY_ATTRIBUTES); // nLength = sizeof(SECURITY_ATTRIBUTES)
2386 Marshal.WriteIntPtr(pSecurityAttributes, 4, pSecurityDescriptor); // lpSecurityDescriptor = pSecurityDescriptor
2387 Marshal.WriteInt32(pSecurityAttributes, 8, 0); // bInheritHandle = FALSE
2388 hFileMap = NativeMethods.CreateFileMappingA(
2389 ADP.InvalidPtr/*INVALID_HANDLE_VALUE*/,
2390 pSecurityAttributes,
2391 0x4/*PAGE_READWRITE*/,
2393 Marshal.SizeOf(typeof(MEMMAP)),
2396 if (IntPtr.Zero == hFileMap) {
2401 pMemMap = NativeMethods.MapViewOfFile(hFileMap, 0x6/*FILE_MAP_READ|FILE_MAP_WRITE*/, 0, 0, IntPtr.Zero);
2403 if (IntPtr.Zero == pMemMap) {
2407 // copy data to memory-mapped file
2408 // layout of MEMMAP structure is:
2411 // byte[32] machineName
2412 // byte[16] sdiDllName
2416 Marshal.WriteInt32(pMemMap, offset, (int)dwpidDebugger);
2418 Marshal.WriteInt32(pMemMap, offset, (int)dwOption);
2420 Marshal.Copy(rgbMachineName, 0, ADP.IntPtrOffset(pMemMap, offset), rgbMachineName.Length);
2421 offset += TdsEnums.SDCI_MAX_MACHINENAME;
2422 Marshal.Copy(rgbSDIDLLName, 0, ADP.IntPtrOffset(pMemMap, offset), rgbSDIDLLName.Length);
2423 offset += TdsEnums.SDCI_MAX_DLLNAME;
2424 Marshal.WriteInt32(pMemMap, offset, (int)cbData);
2426 if (null != rgbData) {
2427 Marshal.Copy(rgbData, 0, ADP.IntPtrOffset(pMemMap, offset), (int)cbData);
2429 NativeMethods.UnmapViewOfFile(pMemMap);
2432 if (result == false) {
2433 if (hFileMap != IntPtr.Zero)
2434 NativeMethods.CloseHandle(hFileMap);
2436 if (pSecurityAttributes != IntPtr.Zero)
2437 Marshal.FreeHGlobal(pSecurityAttributes);
2438 if (pSecurityDescriptor != IntPtr.Zero)
2439 Marshal.FreeHGlobal(pSecurityDescriptor);
2440 if (pDacl != IntPtr.Zero)
2441 Marshal.FreeHGlobal(pDacl);
2446 // this is a private interface to com+ users
2447 // do not change this guid
2451 Guid("6cb925bf-c3c0-45b3-9f44-5dd67c7b7fe8"),
2452 InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
2453 BestFitMapping(false, ThrowOnUnmappableChar = true),
2455 interface ISQLDebug {
2457 [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
2461 [MarshalAs(UnmanagedType.LPStr)] string pszMachineName,
2462 [MarshalAs(UnmanagedType.LPStr)] string pszSDIDLLName,
2468 sealed class SqlDebugContext: IDisposable {
2470 internal uint pid = 0;
2471 internal uint tid = 0;
2472 internal bool active = false;
2473 // memory-mapped data
2474 internal IntPtr pMemMap = ADP.PtrZero;
2475 internal IntPtr hMemMap = ADP.PtrZero;
2476 internal uint dbgpid = 0;
2477 internal bool fOption = false;
2478 internal string machineName = null;
2479 internal string sdiDllName = null;
2480 internal byte[] data = null;
2482 public void Dispose() {
2484 GC.SuppressFinalize(this);
2487 // using CloseHandle and UnmapViewOfFile - no exposure
2488 [ResourceExposure(ResourceScope.None)]
2489 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
2490 private void Dispose(bool disposing) {
2492 // Nothing to do here
2495 if (pMemMap != IntPtr.Zero) {
2496 NativeMethods.UnmapViewOfFile(pMemMap);
2497 pMemMap = IntPtr.Zero;
2499 if (hMemMap != IntPtr.Zero) {
2500 NativeMethods.CloseHandle(hMemMap);
2501 hMemMap = IntPtr.Zero;
2506 ~SqlDebugContext() {
2512 // native interop memory mapped structure for sdi debugging
2513 [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
2514 internal struct MEMMAP {
2515 [MarshalAs(UnmanagedType.U4)]
2516 internal uint dbgpid; // id of debugger
2517 [MarshalAs(UnmanagedType.U4)]
2518 internal uint fOption; // 1 - start debugging, 0 - stop debugging
2519 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
2520 internal byte[] rgbMachineName;
2521 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2522 internal byte[] rgbDllName;
2523 [MarshalAs(UnmanagedType.U4)]
2524 internal uint cbData;
2525 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
2526 internal byte[] rgbData;
2528 } // System.Data.SqlClient namespace