Skip to content

Commit ab024df

Browse files
Fix unknown transaction state issues when promoting delegated transaction(Backport dotnet#1216)
1 parent 8908b92 commit ab024df

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,22 @@ public byte[] Promote()
208208
}
209209

210210
//Throw exception only if Transaction is still active and not yet aborted.
211-
if (promoteException != null && Transaction.TransactionInformation.Status != TransactionStatus.Aborted)
211+
if (promoteException != null)
212212
{
213-
throw SQL.PromotionFailed(promoteException);
213+
try
214+
{
215+
// Safely access Transction status - as it's possible Transaction is not in right state.
216+
if (Transaction?.TransactionInformation?.Status != TransactionStatus.Aborted)
217+
{
218+
throw SQL.PromotionFailed(promoteException);
219+
}
220+
}
221+
catch (TransactionException te)
222+
{
223+
SqlClientEventSource.Log.TryTraceEvent("SqlDelegatedTransaction.Promote | RES | CPOOL | Object Id {0}, Client Connection Id {1}, Transaction exception occurred: {2}.", ObjectID, usersConnection?.ClientConnectionId, te.Message);
224+
// Throw promote exception if transaction state is unknown.
225+
throw SQL.PromotionFailed(promoteException);
226+
}
214227
}
215228
else
216229
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,22 @@ public Byte[] Promote()
255255
}
256256

257257
//Throw exception only if Transaction is still active and not yet aborted.
258-
if (promoteException != null && Transaction.TransactionInformation.Status != SysTx.TransactionStatus.Aborted)
258+
if (promoteException != null)
259259
{
260-
throw SQL.PromotionFailed(promoteException);
260+
try
261+
{
262+
// Safely access Transction status - as it's possible Transaction is not in right state.
263+
if (Transaction?.TransactionInformation?.Status == SysTx.TransactionStatus.Aborted)
264+
{
265+
throw SQL.PromotionFailed(promoteException);
266+
}
267+
}
268+
catch (SysTx.TransactionException te)
269+
{
270+
SqlClientEventSource.Log.TryTraceEvent("SqlDelegatedTransaction.Promote | RES | CPOOL | Object Id {0}, Client Connection Id {1}, Transaction exception occurred: {2}.", ObjectID, usersConnection?.ClientConnectionId, te.Message);
271+
// Throw promote exception if transaction state is unknown.
272+
throw SQL.PromotionFailed(promoteException);
273+
}
261274
}
262275
else
263276
{

0 commit comments

Comments
 (0)