private void AddToConnectionInfoTable(Transaction transaction, SharedConnectionInfo connectionInfo) { lock (this.tableSyncObject) { this.transactionConnectionTable.Add(transaction, connectionInfo); WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, string.Concat(new object[] { "AddToConnectionInfoTable ", transaction.GetHashCode(), " in table of count ", this.transactionConnectionTable.Count })); } }
private void AddToConnectionInfoTable(Transaction transaction, SharedConnectionInfo connectionInfo) { lock (this.tableSyncObject) { this.transactionConnectionTable.Add(transaction, connectionInfo); WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "AddToConnectionInfoTable " + transaction.GetHashCode() + " in table of count " + this.transactionConnectionTable.Count); } }
internal static DbTransaction GetLocalTransaction(WorkflowCommitWorkBatchService txSvc, Transaction transaction) { DbTransaction dBTransaction = null; SharedConnectionInfo connectionInfo = GetConnectionInfo(txSvc, transaction); if (connectionInfo != null) { dBTransaction = connectionInfo.DBTransaction; } return(dBTransaction); }
private static SharedConnectionInfo GetConnectionInfo(WorkflowCommitWorkBatchService txSvc, Transaction transaction) { SharedConnectionInfo connectionInfo = null; SharedConnectionWorkflowCommitWorkBatchService service = txSvc as SharedConnectionWorkflowCommitWorkBatchService; if (service != null) { connectionInfo = service.GetConnectionInfo(transaction); if (connectionInfo == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExecutionStringManager.InvalidTransaction, new object[0])); } } return(connectionInfo); }
internal DbConnection GetEnlistedConnection(WorkflowCommitWorkBatchService txSvc, Transaction transaction, out bool isNewConnection) { DbConnection dBConnection; SharedConnectionInfo connectionInfo = GetConnectionInfo(txSvc, transaction); if (connectionInfo != null) { dBConnection = connectionInfo.DBConnection; isNewConnection = false; return(dBConnection); } dBConnection = this.OpenNewConnection(); dBConnection.EnlistTransaction(transaction); isNewConnection = true; return(dBConnection); }
/* * private void SetLocalProvider(string connectionString) * { * // Assume caller already validated the connection string * MatchCollection providers = Regex.Matches(connectionString, @"(^|;)\s*provider\s*=[^;$]*(;|$)", RegexOptions.IgnoreCase); * * // Cannot use DbConnectionStringBuilder because it selects the last provider, not the first one, by itself. * // A legal Sql connection string allows for multiple provider specification and * // selects the first provider * if (providers.Count > 0) * { * // Check if the first one matches "sqloledb" or "sqloledb.<digit>" * if (Regex.IsMatch(providers[0].Value, @"provider\s*=\s*sqloledb(\.\d+)?\s*(;|$)", RegexOptions.IgnoreCase)) * { * this.localProvider = Provider.OleDB; * } * else * { * // We don't support other providers * throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,ExecutionStringManager.UnsupportedSqlProvider, providers[0].Value)); * } * } * else * { * // SqlClient provider requires no provider keyword specified in connection string * this.localProvider = Provider.SqlClient; * } * } */ private static SharedConnectionInfo GetConnectionInfo(WorkflowCommitWorkBatchService txSvc, Transaction transaction) { SharedConnectionInfo connectionInfo = null; SharedConnectionWorkflowCommitWorkBatchService scTxSvc = txSvc as SharedConnectionWorkflowCommitWorkBatchService; if (scTxSvc != null) { connectionInfo = scTxSvc.GetConnectionInfo(transaction); // The transaction service can't find entry if the transaction has been completed. // be sure to propate the error so durable services can cast to appropriate exception if (connectionInfo == null) { throw new ArgumentException( String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.InvalidTransaction)); } } return(connectionInfo); }
/// <summary> /// Gets a connection enlisted to the transaction. /// If the transaction already has a connection attached to it, we return that, /// otherwise we create a new connection and enlist to the transaction /// </summary> /// <param name="transaction"></param> /// <param name="isNewConnection">output if we created a connection</param> /// <returns></returns> internal DbConnection GetEnlistedConnection(WorkflowCommitWorkBatchService txSvc, Transaction transaction, out bool isNewConnection) { DbConnection connection; SharedConnectionInfo connectionInfo = GetConnectionInfo(txSvc, transaction); if (connectionInfo != null) { connection = connectionInfo.DBConnection; Debug.Assert((connection != null), "null connection"); Debug.Assert((connection.State == System.Data.ConnectionState.Open), "Invalid connection state " + connection.State + " for connection " + connection); isNewConnection = false; } else { connection = this.OpenNewConnection(); connection.EnlistTransaction(transaction); isNewConnection = true; } return(connection); }
protected internal override void CommitWorkBatch(WorkflowCommitWorkBatchService.CommitWorkBatchCallback commitWorkBatchCallback) { // // Disable retries by default, reset to allow retries below if we own the tx DbRetry dbRetry = new DbRetry(_enableRetries); short retryCounter = dbRetry.MaxRetries; while (true) { // // When using LocalTransaction handle block access to the connection // in the transaction event handlers until all IPendingWork members have completed ManualResetEvent handle = new ManualResetEvent(false); Transaction tx = null; SharedConnectionInfo connectionInfo = null; try { if (null == Transaction.Current) { // // It's OK to retry here as we own the tx retryCounter = 0; // // Create a local, non promotable transaction that we share with our OOB services tx = new CommittableTransaction(); connectionInfo = new SharedConnectionInfo(this.dbResourceAllocator, tx, false, handle); } else { // // Can't retry as we don't own the tx // Create a dependent transaction and don't restrict promotion. tx = Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete); connectionInfo = new SharedConnectionInfo(this.dbResourceAllocator, tx, true, handle); } AddToConnectionInfoTable(tx, connectionInfo); using (TransactionScope ts = new TransactionScope(tx)) { try { commitWorkBatchCallback(); ts.Complete(); } finally { RemoveConnectionFromInfoTable(tx); // // Unblock transaction event handlers handle.Set(); } } CommittableTransaction committableTransaction = tx as CommittableTransaction; if (committableTransaction != null) committableTransaction.Commit(); DependentTransaction dependentTransaction = tx as DependentTransaction; if (dependentTransaction != null) dependentTransaction.Complete(); break; } catch (Exception e) { tx.Rollback(); WorkflowTrace.Host.TraceEvent(TraceEventType.Error, 0, "SharedConnectionWorkflowCommitWorkBatchService caught exception from commitWorkBatchCallback: " + e.ToString()); if (dbRetry.TryDoRetry(ref retryCounter)) { WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "SharedConnectionWorkflowCommitWorkBatchService retrying commitWorkBatchCallback (retry attempt " + retryCounter.ToString(System.Globalization.CultureInfo.InvariantCulture) + ")"); continue; } else throw; } finally { handle.Close(); if (tx != null) { tx.Dispose(); } } } }
protected internal override void CommitWorkBatch(WorkflowCommitWorkBatchService.CommitWorkBatchCallback commitWorkBatchCallback) { // // Disable retries by default, reset to allow retries below if we own the tx DbRetry dbRetry = new DbRetry(_enableRetries); short retryCounter = dbRetry.MaxRetries; while (true) { // // When using LocalTransaction handle block access to the connection // in the transaction event handlers until all IPendingWork members have completed ManualResetEvent handle = new ManualResetEvent(false); Transaction tx = null; SharedConnectionInfo connectionInfo = null; try { if (null == Transaction.Current) { // // It's OK to retry here as we own the tx retryCounter = 0; // // Create a local, non promotable transaction that we share with our OOB services tx = new CommittableTransaction(); connectionInfo = new SharedConnectionInfo(this.dbResourceAllocator, tx, false, handle); } else { // // Can't retry as we don't own the tx // Create a dependent transaction and don't restrict promotion. tx = Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete); connectionInfo = new SharedConnectionInfo(this.dbResourceAllocator, tx, true, handle); } AddToConnectionInfoTable(tx, connectionInfo); using (TransactionScope ts = new TransactionScope(tx)) { try { commitWorkBatchCallback(); ts.Complete(); } finally { RemoveConnectionFromInfoTable(tx); // // Unblock transaction event handlers handle.Set(); } } CommittableTransaction committableTransaction = tx as CommittableTransaction; if (committableTransaction != null) { committableTransaction.Commit(); } DependentTransaction dependentTransaction = tx as DependentTransaction; if (dependentTransaction != null) { dependentTransaction.Complete(); } break; } catch (Exception e) { tx.Rollback(); WorkflowTrace.Host.TraceEvent(TraceEventType.Error, 0, "SharedConnectionWorkflowCommitWorkBatchService caught exception from commitWorkBatchCallback: " + e.ToString()); if (dbRetry.TryDoRetry(ref retryCounter)) { WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "SharedConnectionWorkflowCommitWorkBatchService retrying commitWorkBatchCallback (retry attempt " + retryCounter.ToString(System.Globalization.CultureInfo.InvariantCulture) + ")"); continue; } else { throw; } } finally { handle.Close(); if (tx != null) { tx.Dispose(); } } } }
protected internal override void CommitWorkBatch(WorkflowCommitWorkBatchService.CommitWorkBatchCallback commitWorkBatchCallback) { ManualResetEvent event2; DbRetry retry = new DbRetry(this._enableRetries); short maxRetries = retry.MaxRetries; Label_0013: event2 = new ManualResetEvent(false); Transaction transaction = null; SharedConnectionInfo connectionInfo = null; try { if (null == Transaction.Current) { maxRetries = 0; transaction = new CommittableTransaction(); connectionInfo = new SharedConnectionInfo(this.dbResourceAllocator, transaction, false, event2); } else { transaction = Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete); connectionInfo = new SharedConnectionInfo(this.dbResourceAllocator, transaction, true, event2); } this.AddToConnectionInfoTable(transaction, connectionInfo); using (TransactionScope scope = new TransactionScope(transaction)) { try { commitWorkBatchCallback(); scope.Complete(); } finally { this.RemoveConnectionFromInfoTable(transaction); event2.Set(); } } CommittableTransaction transaction2 = transaction as CommittableTransaction; if (transaction2 != null) { transaction2.Commit(); } DependentTransaction transaction3 = transaction as DependentTransaction; if (transaction3 != null) { transaction3.Complete(); } } catch (Exception exception) { transaction.Rollback(); WorkflowTrace.Host.TraceEvent(TraceEventType.Error, 0, "SharedConnectionWorkflowCommitWorkBatchService caught exception from commitWorkBatchCallback: " + exception.ToString()); if (!retry.TryDoRetry(ref maxRetries)) { throw; } WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "SharedConnectionWorkflowCommitWorkBatchService retrying commitWorkBatchCallback (retry attempt " + maxRetries.ToString(CultureInfo.InvariantCulture) + ")"); goto Label_0013; } finally { event2.Close(); if (transaction != null) { transaction.Dispose(); } } }