public void Enlist(Transaction tx) { 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; } } }
public void Rollback(SinglePhaseEnlistment singlePhaseEnlistment) { // 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(); } else if (_callbacks != null) { if (_rm != null) { _rm.RollbackWork(_callbacks.GetName()); singlePhaseEnlistment.Aborted(); } else { _callbacks.RollbackTransaction(); singlePhaseEnlistment.Aborted(); } _callbacks = null; } _inTransaction = false; }
public void Initialize() { _npgsqlTx = _connection.BeginTransaction(ConvertIsolationLevel(_isolationLevel)); _inTransaction = true; }
/// <summary> /// Used when a connection is closed /// </summary> public void 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; } } }
public byte[] 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; } return token; }
public void SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment) { if (_npgsqlTx != null) { _npgsqlTx.Commit(); _npgsqlTx.Dispose(); _npgsqlTx = null; singlePhaseEnlistment.Committed(); } else if (_callbacks != null) { if (_rm != null) { _rm.CommitWork(_callbacks.GetName()); singlePhaseEnlistment.Committed(); } else { _callbacks.CommitTransaction(); singlePhaseEnlistment.Committed(); } _callbacks = null; } _inTransaction = false; }
/// <summary> /// Initializes a new instance of the <see cref="Npgsql.NpgsqlCommand">NpgsqlCommand</see> class with the text of the query, a <see cref="Npgsql.NpgsqlConnection">NpgsqlConnection</see>, and the <see cref="Npgsql.NpgsqlTransaction">NpgsqlTransaction</see>. /// </summary> /// <param name="cmdText">The text of the query.</param> /// <param name="connection">A <see cref="Npgsql.NpgsqlConnection">NpgsqlConnection</see> that represents the connection to a PostgreSQL server.</param> /// <param name="transaction">The <see cref="Npgsql.NpgsqlTransaction">NpgsqlTransaction</see> in which the <see cref="Npgsql.NpgsqlCommand">NpgsqlCommand</see> executes.</param> public NpgsqlCommand(String cmdText, NpgsqlConnection connection, NpgsqlTransaction transaction) { parameters = new NpgsqlParameterCollection(); planName = String.Empty; text = cmdText; this.connection = connection; if (this.connection != null) { this.m_Connector = connection.Connector; } type = CommandType.Text; this.Transaction = transaction; SetCommandTimeout(); }