Skip to content

Commit aa7a826

Browse files
committed
Set Connection to null when finished. Fixes #61
This is a breaking change with Connector/NET but more standard compared to other ADO.NET providers.
1 parent 5c84fc7 commit aa7a826

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlTransaction.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ public sealed class MySqlTransaction : DbTransaction
1212
public override void Commit() =>
1313
CommitAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
1414

15-
public Task CommitAsync(CancellationToken cancellationToken = default(CancellationToken)) =>
16-
CommitAsync(m_connection.AsyncIOBehavior, cancellationToken);
15+
public Task CommitAsync(CancellationToken cancellationToken = default) =>
16+
CommitAsync(m_connection?.AsyncIOBehavior ?? IOBehavior.Asynchronous, cancellationToken);
1717

1818
internal async Task CommitAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
1919
{
2020
VerifyNotDisposed();
21-
if (m_isFinished)
21+
if (m_connection == null)
2222
throw new InvalidOperationException("Already committed or rolled back.");
2323

2424
if (m_connection.CurrentTransaction == this)
2525
{
2626
using (var cmd = new MySqlCommand("commit", m_connection, this))
2727
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
2828
m_connection.CurrentTransaction = null;
29-
m_isFinished = true;
29+
m_connection = null;
3030
}
3131
else if (m_connection.CurrentTransaction != null)
3232
{
@@ -41,21 +41,21 @@ internal async Task CommitAsync(IOBehavior ioBehavior, CancellationToken cancell
4141
public override void Rollback() =>
4242
RollbackAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
4343

44-
public Task RollbackAsync(CancellationToken cancellationToken = default(CancellationToken)) =>
45-
RollbackAsync(m_connection.AsyncIOBehavior, cancellationToken);
44+
public Task RollbackAsync(CancellationToken cancellationToken = default) =>
45+
RollbackAsync(m_connection?.AsyncIOBehavior ?? IOBehavior.Asynchronous, cancellationToken);
4646

4747
internal async Task RollbackAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
4848
{
4949
VerifyNotDisposed();
50-
if (m_isFinished)
50+
if (m_connection == null)
5151
throw new InvalidOperationException("Already committed or rolled back.");
5252

5353
if (m_connection.CurrentTransaction == this)
5454
{
5555
using (var cmd = new MySqlCommand("rollback", m_connection, this))
5656
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
5757
m_connection.CurrentTransaction = null;
58-
m_isFinished = true;
58+
m_connection = null;
5959
}
6060
else if (m_connection.CurrentTransaction != null)
6161
{
@@ -77,7 +77,8 @@ protected override void Dispose(bool disposing)
7777
{
7878
if (disposing)
7979
{
80-
if (!m_isFinished && m_connection?.CurrentTransaction == this)
80+
m_isDisposed = true;
81+
if (m_connection?.CurrentTransaction == this)
8182
{
8283
if (m_connection.Session.IsConnected)
8384
{
@@ -103,11 +104,11 @@ internal MySqlTransaction(MySqlConnection connection, IsolationLevel isolationLe
103104

104105
private void VerifyNotDisposed()
105106
{
106-
if (m_connection == null)
107+
if (m_isDisposed)
107108
throw new ObjectDisposedException(nameof(MySqlTransaction));
108109
}
109110

110111
MySqlConnection m_connection;
111-
bool m_isFinished;
112+
bool m_isDisposed;
112113
}
113114
}

0 commit comments

Comments
 (0)