Exemple #1
0
        /// roll back the transaction
        public void Rollback()
        {
            string         TransactionIdentifier     = null;
            bool           TransactionValid          = false;
            IsolationLevel TransactionIsolationLevel = IsolationLevel.Unspecified;

            // Attempt to roll back the DB Transaction.
            try
            {
                // 'Sanity Check': Check that TheTransaction hasn't been committed or rolled back yet.
                if (!Valid)
                {
                    var Exc1 =
                        new EDBAttemptingToUseTransactionThatIsInvalidException(
                            "TDataBase.RollbackTransaction called on DB Transaction that isn't valid",
                            ThreadingHelper.GetThreadIdentifier(ThreadThatTransactionWasStartedOn),
                            ThreadingHelper.GetCurrentThreadIdentifier());

                    TLogging.Log(Exc1.ToString());

                    throw Exc1;
                }

                if (TLogging.DL >= DBAccess.DB_DEBUGLEVEL_TRANSACTION_DETAIL)
                {
                    // Gather information for logging
                    TransactionIdentifier     = GetDBTransactionIdentifier();
                    TransactionValid          = Valid;
                    TransactionIsolationLevel = IsolationLevel;
                }

                WrappedTransaction.Rollback();

                TLogging.LogAtLevel(DBAccess.DB_DEBUGLEVEL_TRANSACTION, "       Transaction " + FTransactionName + " in Connection " + FTDataBaseInstanceThatTransactionBelongsTo.ConnectionName + " rolled back");

                // Rollback was OK, now clean up.
                Dispose();

                TLogging.LogAtLevel(DBAccess.DB_DEBUGLEVEL_TRANSACTION_DETAIL, String.Format(
                                        "DB Transaction{0} got rolled back.  Before that, its DB Transaction Properties were: Valid: {1}, " +
                                        "IsolationLevel: {2} (it got started on Thread {3} in AppDomain '{4}').", TransactionIdentifier,
                                        TransactionValid, TransactionIsolationLevel, ThreadThatTransactionWasStartedOn,
                                        AppDomainNameThatTransactionWasStartedIn)
                                    );
            }
            catch (Exception Exc)
            {
                // This catch block will handle any errors that may have occurred
                // on the server that would cause the rollback to fail, such as
                // a closed connection.
                //
                // MSDN says: "Try/Catch exception handling should always be used when rolling back a
                // transaction. A Rollback generates an InvalidOperationException if the connection is
                // terminated or if the transaction has already been rolled back on the server."
                TLogging.Log("Exception while attempting Transaction rollback: " + Exc.ToString());
            }
        }