Skip to content

Commit

Permalink
Add xa branching transaction preparation exception handling. Fixes #1348
Browse files Browse the repository at this point in the history
  • Loading branch information
JackBOBO committed Jul 28, 2023
1 parent 81e2e3a commit de8c643
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/MySqlConnector/Core/EnlistedTransactionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ internal abstract class EnlistedTransactionBase : IEnlistmentNotification
// Whether the connection is idle, i.e., a client has closed it and is no longer using it
public bool IsIdle { get; set; }

// Whether to enter the PREPARED state, The rollback operation needs to determine if the Xa transaction has entered the PREPARED state.
public bool IsPrepared { get; set; }

public Transaction Transaction { get; private set; }

public void Start()
Expand All @@ -20,8 +23,16 @@ public void Start()

void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
{
OnPrepare(preparingEnlistment);
preparingEnlistment.Prepared();
try
{
OnPrepare(preparingEnlistment);
IsPrepared = true;
preparingEnlistment.Prepared();
}
catch (Exception ex)
{
preparingEnlistment.ForceRollback(ex);
}
}

void IEnlistmentNotification.Commit(Enlistment enlistment)
Expand Down
4 changes: 3 additions & 1 deletion src/MySqlConnector/Core/XaEnlistedTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ protected override void OnRollback(Enlistment enlistment)
{
try
{
ExecuteXaCommand("END");
if (!IsPrepared)
ExecuteXaCommand("END");

ExecuteXaCommand("ROLLBACK");
}
catch (MySqlException ex) when (ex.ErrorCode is MySqlErrorCode.XARBDeadlock)
Expand Down

0 comments on commit de8c643

Please sign in to comment.