public void Prepare(PreparingEnlistment preparingEnlistment)
{
try
{
dtcControlEvent.Reset();
Tracer.Debug("Prepare notification received for TX id: " + this.transactionId);
BeforeEnd();
// Before sending the request to the broker, log the recovery bits, if
// this fails we can't prepare and the TX should be rolled back.
RecoveryLogger.LogRecoveryInfo(this.transactionId as XATransactionId,
preparingEnlistment.RecoveryInformation());
// Inform the broker that work on the XA'sh TX Branch is complete.
TransactionInfo info = new TransactionInfo();
info.ConnectionId = this.connection.ConnectionId;
info.TransactionId = this.transactionId;
info.Type = (int) TransactionType.End;
this.connection.CheckConnected();
this.connection.SyncRequest(info);
// Prepare the Transaction for commit.
info.Type = (int) TransactionType.Prepare;
IntegerResponse response = (IntegerResponse) this.connection.SyncRequest(info);
if(response.Result == XA_READONLY)
{
Tracer.Debug("Transaction Prepare done and doesn't need a commit, TX id: " + this.transactionId);
this.transactionId = null;
this.currentEnlistment = null;
// Read Only means there's nothing to recover because there was no
// change on the broker.
RecoveryLogger.LogRecovered(this.transactionId as XATransactionId);
// if server responds that nothing needs to be done, then reply prepared
// but clear the current state data so we appear done to the commit method.
preparingEnlistment.Prepared();
// Done so commit won't be called.
AfterCommit();
}
else
{
Tracer.Debug("Transaction Prepare succeeded TX id: " + this.transactionId);
// If work finished correctly, reply prepared
preparingEnlistment.Prepared();
}
}
catch(Exception ex)
{
Tracer.DebugFormat("Transaction[{0}] Prepare failed with error: {1}",
this.transactionId, ex.Message);
AfterRollback();
preparingEnlistment.ForceRollback();
try
{
this.connection.OnException(ex);
}
catch (Exception error)
{
Tracer.Error(error.ToString());
}
this.currentEnlistment = null;
this.transactionId = null;
}
finally
{
this.dtcControlEvent.Set();
}
}