Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release 3.0] Fix unknown transaction state issues when promoting delegated transaction #1247

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public byte[] Promote()
{
promoteException = e;

ADP.TraceExceptionWithoutRethrow(e);

// Doom the connection, to make sure that the transaction is
// eventually rolled back.
// VSTS 144562: doom the connection while having the lock on it to prevent race condition with "Transaction Ended" Event
Expand All @@ -187,6 +189,7 @@ public byte[] Promote()
catch (InvalidOperationException e)
{
promoteException = e;
ADP.TraceExceptionWithoutRethrow(e);
connection.DoomThisConnection();
}
}
Expand All @@ -208,9 +211,22 @@ public byte[] Promote()
}

//Throw exception only if Transaction is still active and not yet aborted.
if (promoteException != null && Transaction.TransactionInformation.Status != TransactionStatus.Aborted)
if (promoteException != null)
{
throw SQL.PromotionFailed(promoteException);
try
{
// Safely access Transaction status - as it's possible Transaction is not in right state.
if (Transaction?.TransactionInformation?.Status != TransactionStatus.Aborted)
{
throw SQL.PromotionFailed(promoteException);
}
}
catch (TransactionException te)
{
SqlClientEventSource.Log.TryTraceEvent("SqlDelegatedTransaction.Promote | RES | CPOOL | Object Id {0}, Client Connection Id {1}, Transaction exception occurred: {2}.", ObjectID, usersConnection?.ClientConnectionId, te.Message);
// Throw promote exception if transaction state is unknown.
throw SQL.PromotionFailed(promoteException);
}
}
else
{
Expand Down Expand Up @@ -354,6 +370,8 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
{
commitException = e;

ADP.TraceExceptionWithoutRethrow(e);

// Doom the connection, to make sure that the transaction is
// eventually rolled back.
// VSTS 144562: doom the connection while having the lock on it to prevent race condition with "Transaction Ended" Event
Expand All @@ -362,6 +380,7 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
catch (InvalidOperationException e)
{
commitException = e;
ADP.TraceExceptionWithoutRethrow(e);
connection.DoomThisConnection();
}
if (commitException != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,22 @@ public Byte[] Promote()
}

//Throw exception only if Transaction is still active and not yet aborted.
if (promoteException != null && Transaction.TransactionInformation.Status != SysTx.TransactionStatus.Aborted)
if (promoteException != null)
{
throw SQL.PromotionFailed(promoteException);
try
{
// Safely access Transction status - as it's possible Transaction is not in right state.
if (Transaction?.TransactionInformation?.Status == SysTx.TransactionStatus.Aborted)
{
throw SQL.PromotionFailed(promoteException);
}
}
catch (SysTx.TransactionException te)
{
SqlClientEventSource.Log.TryTraceEvent("SqlDelegatedTransaction.Promote | RES | CPOOL | Object Id {0}, Client Connection Id {1}, Transaction exception occurred: {2}.", ObjectID, usersConnection?.ClientConnectionId, te.Message);
// Throw promote exception if transaction state is unknown.
throw SQL.PromotionFailed(promoteException);
}
}
else
{
Expand Down