protected internal SqlTransactionScopeLocal(ShardLocation location, SqlConnectionStringBuilder shardConnectionString) { Debug.Assert(SqlTransactionScopeLocal.current == null); Debug.Assert(location != null); Debug.Assert(location.DataSource != null); Debug.Assert(location.Database != null); // Copy the builder. SqlConnectionStringBuilder lcsb = new SqlConnectionStringBuilder(shardConnectionString.ConnectionString); lcsb.DataSource = location.DataSource; lcsb.InitialCatalog = location.Database; SqlConnection conn = new SqlConnection(lcsb.ConnectionString); conn.Open(); SqlTransaction tran = conn.BeginTransaction(IsolationLevel.Serializable); this.Location = location; this.conn = conn; this.tran = tran; SqlTransactionScopeLocal.current = this; }
/// <summary> /// Completes the currently active transaction. /// </summary> internal void FinishTransaction() { Debug.Assert(this.conn != null); Debug.Assert(this.tran != null); try { #if DEBUG // Raise event and check if current transaction should be aborted. // SqlStoreEventArgs eventArgs = new SqlStoreEventArgs(); OnDisposeEvent(eventArgs); if (eventArgs.action == SqlStoreEventArgs.SqlStoreTxnFinishAction.TxnAbort) { throw new StoreException( Errors.SqlTransactionScopeLocal_SqlException, this.Success ? "Commit" : "Rollback", this.Location); } else #endif // DEBUG { if (this.Success) { this.tran.Commit(); } else { this.tran.Rollback(); } } } catch (SqlException se) { throw new StoreException( Errors.SqlTransactionScopeLocal_SqlException, se, this.Success ? "Commit" : "Rollback", this.Location); } finally { this.tran.Dispose(); this.conn.Dispose(); SqlTransactionScopeLocal.current = null; } }
public virtual IStoreTransactionScope GetTransactionScopeLocal(ShardLocation location) { try { SqlTransactionScopeLocal txnScopeLocal = new SqlTransactionScopeLocal(location, this.credentials.ConnectionStringShard); #if DEBUG EventHandler<SqlStoreEventArgs> handler = this.SqlStoreEventLocal; if (handler != null) { // If there are any subscribers for SqlStoreEventLocal event, then register a subscriber function with SqlTransactionScopeGlobal // txnScopeLocal.TxnScopeLocalDisposeEvent += TxnScopeLocalEventHandler; } #endif // DEBUG return txnScopeLocal; } catch (SqlException se) { throw new StoreException( Errors.SqlStore_GetTransactionScopeLocal_SqlException, se, location); } }