public void SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
 {
     NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "SinglePhaseCommit");
     if (_npgsqlTx != null)
     {
         _npgsqlTx.Commit();
         _npgsqlTx.Dispose();
         _npgsqlTx = null;
         singlePhaseEnlistment.Committed();
         _connection.PromotableLocalTransactionEnded();
     }
     else if (_callbacks != null)
     {
         if (_rm != null)
         {
             _rm.CommitWork(_callbacks.GetName());
             singlePhaseEnlistment.Committed();
         }
         else
         {
             _callbacks.CommitTransaction();
             singlePhaseEnlistment.Committed();
         }
         _callbacks = null;
     }
     _inTransaction = false;
 }
 public void Rollback(SinglePhaseEnlistment singlePhaseEnlistment)
 {
     NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Rollback");
     // try to rollback the transaction with either the
     // ADO.NET transaction or the callbacks that managed the
     // two phase commit transaction.
     if (_npgsqlTx != null)
     {
         _npgsqlTx.Rollback();
         _npgsqlTx.Dispose();
         _npgsqlTx = null;
         singlePhaseEnlistment.Aborted();
         _connection.PromotableLocalTransactionEnded();
     }
     else if (_callbacks != null)
     {
         if (_rm != null)
         {
             _rm.RollbackWork(_callbacks.GetName());
             singlePhaseEnlistment.Aborted();
         }
         else
         {
             _callbacks.RollbackTransaction();
             singlePhaseEnlistment.Aborted();
         }
         _callbacks = null;
     }
     _inTransaction = false;
 }
 /// <summary>
 /// Used when a connection is closed
 /// </summary>
 public void Prepare()
 {
     NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Prepare");
     if (_inTransaction)
     {
         // may not be null if Promote or Enlist is called first
         if (_callbacks == null)
         {
             _callbacks = new NpgsqlTransactionCallbacks(_connection);
         }
         _callbacks.PrepareTransaction();
         if (_npgsqlTx != null)
         {
             // cancel the NpgsqlTransaction since this will
             // be handled by a two phase commit.
             _npgsqlTx.Cancel();
             _npgsqlTx.Dispose();
             _npgsqlTx = null;
             _connection.PromotableLocalTransactionEnded();
         }
     }
 }
 public byte[] Promote()
 {
     NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Promote");
     _rm = CreateResourceManager();
     // may not be null if Prepare or Enlist is called first
     if (_callbacks == null)
     {
         _callbacks = new NpgsqlTransactionCallbacks(_connection);
     }
     byte[] token = _rm.Promote(_callbacks);
     // mostly likely case for this is the transaction has been prepared.
     if (_npgsqlTx != null)
     {
         // cancel the NpgsqlTransaction since this will
         // be handled by a two phase commit.
         _npgsqlTx.Cancel();
         _npgsqlTx.Dispose();
         _npgsqlTx = null;
         _connection.PromotableLocalTransactionEnded();
     }
     return(token);
 }
 public void Enlist(Transaction tx)
 {
     NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Enlist");
     if (tx != null)
     {
         _isolationLevel = tx.IsolationLevel;
         if (!tx.EnlistPromotableSinglePhase(this))
         {
             // must already have a durable resource
             // start transaction
             _npgsqlTx      = _connection.BeginTransaction(ConvertIsolationLevel(_isolationLevel));
             _inTransaction = true;
             _rm            = CreateResourceManager();
             _callbacks     = new NpgsqlTransactionCallbacks(_connection);
             _rm.Enlist(_callbacks, TransactionInterop.GetTransmitterPropagationToken(tx));
             // enlisted in distributed transaction
             // disconnect and cleanup local transaction
             _npgsqlTx.Cancel();
             _npgsqlTx.Dispose();
             _npgsqlTx = null;
         }
     }
 }