Exemple #1
0
        /// <inheritdoc/>
        public void Enlist(PrepareCommandDelegate command,
                           ExecuteCommandDelegate executor)
        {
            var enlisted = new EnlistedCommand(delegate(ITransactionContext context)
            {
                var cmd = command(context);
                if (cmd.Connection != connection_)
                {
                    throw new ArgumentException(
                        Resources.Resources.Arg_TransactionContext_Command_Connection);
                }
                return(cmd);
            }, executor);

            commands_.Enqueue(enlisted);
        }
Exemple #2
0
        /// <inheritdoc/>
        public virtual void Complete()
        {
            bool should_close_connection = false;

            try {
                if (connection_.State != ConnectionState.Open)
                {
                    should_close_connection = true;
                    connection_.Open();
                }

                IDbTransaction transaction = null;
                using (transaction = connection_.BeginTransaction()) {
                    while (commands_.Count > 0)
                    {
                        EnlistedCommand enlisted = commands_.Dequeue();
                        IDbCommand      command  = enlisted.Prepare(this);
                        command.Transaction = transaction;
                        enlisted.Execute(command);
                        command.Dispose();
                    }

                    // Attempt to commit the transaction. Both commit and rollback
                    // operations should be enclosed by a try/catch block because they
                    // could generate a Exception if the connection is terminated or
                    // if the transaction has already been rolled back on the server.
                    try {
                        transaction.Commit();
                    } catch (Exception e) {
                        try {
                            transaction.Rollback();
                        } catch (Exception ie) {
                            LogError(ie, "Compete");
                        }
                        LogError(e, "Compete");
                    }
                }
            } finally {
                if (should_close_connection)
                {
                    connection_.Close();
                }
                commands_.Clear();
            }
        }
 /// <inheritdoc/>
 public void Enlist(PrepareCommandDelegate command,
   ExecuteCommandDelegate executor) {
   var enlisted = new EnlistedCommand(delegate(ITransactionContext context)
   {
     var cmd = command(context);
     if (cmd.Connection != connection_) {
       throw new ArgumentException(
         Resources.Resources.Arg_TransactionContext_Command_Connection);
     }
     return cmd;
   }, executor);
   commands_.Enqueue(enlisted);
 }