Exemplo n.º 1
0
        /// <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 ContextNestedWithTwoTransactionLast()
        {
            GlobalTransactionManager.BeginTransaction();
            GlobalTransactionManager.BeginTransaction();

            GlobalTransactionManager.TransactionToken token = GlobalTransactionManager.Enlist(Nope);
            GlobalTransactionManager.TransactionContext.Set("key", "test", 1);
            Assert.That(token.GetFromTransactionContext("key"), Is.EqualTo("test"));
        }
Exemplo n.º 3
0
 /// <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;
         }
     }
 }