/// <summary> /// This is the core execution function, it accept a simple functor that will accept a sqlcommand /// the command is created in the core of the function so it really care of all the standard /// burden of creating connection, creating transaction and enlist command into a transaction. /// </summary> /// <param name="functionToExecute">The delegates that really executes the command.</param> internal static void Execute(Action <DbCommand, DbProviderFactory> functionToExecute) { DbProviderFactory factory = GetFactory(); using (GlobalTransactionManager.TransactionToken token = CreateConnection()) { ConnectionData connectionData = (ConnectionData)token.GetFromTransactionContext(GetKeyFromConnName(String.Empty)); try { using (DbCommand command = factory.CreateCommand()) { command.CommandType = CommandType.Text; command.Connection = connectionData.Connection; command.Transaction = connectionData.CurrentTransaction; functionToExecute(command, factory); } } catch { //There is an exception, I doom the transaction. token.Doom(); throw; } } }
public void EnlistWithoutATransactionRollback() { Action <Boolean> mock = mockRepository.CreateMock <Action <Boolean> >(); mock(false); //Sets the expectation mockRepository.ReplayAll(); using (GlobalTransactionManager.TransactionToken token = GlobalTransactionManager.Enlist(mock)) { token.Doom(); //Do something, the important thing is that the delegate is called because we have //not a transaction active. } }
/// <summary> /// Execute a sqlquery. /// </summary> /// <param name="q"></param> /// <param name="executionCore"></param> public static void Execute(SqlQuery q, Action executionCore) { using (GlobalTransactionManager.TransactionToken token = CreateConnection(q.ConnectionStringName)) { ConnectionData connectionData = (ConnectionData)token.GetFromTransactionContext( GetKeyFromConnName(q.ConnectionStringName)); try { using (q.Command) { q.Command.Connection = connectionData.Connection; q.Command.Transaction = connectionData.CurrentTransaction; q.Command.CommandText = q.Query; executionCore(); } } catch { token.Doom(); throw; } } }