Skip to content

Commit ffea494

Browse files
committed
Add MySqlConnection.ResetConnectionAsync. Fixes #831
1 parent 1ea1362 commit ffea494

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ jobs:
197197
matrix:
198198
'MySQL 5.6':
199199
image: 'mysql:5.6'
200-
unsupportedFeatures: 'Ed25519,Json,Sha256Password,LargePackets,CachingSha2Password,SessionTrack,Tls13,UuidToBin'
200+
unsupportedFeatures: 'Ed25519,Json,Sha256Password,LargePackets,CachingSha2Password,ResetConnection,SessionTrack,Tls13,UuidToBin'
201201
'MySQL 5.7':
202202
image: 'mysql:5.7'
203203
unsupportedFeatures: 'Ed25519,CachingSha2Password,Tls13,UuidToBin'

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,24 @@ private async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancellat
415415
#endif
416416
}
417417

418+
/// <summary>
419+
/// Resets the session state of the current open connection; this clears temporary tables and user-defined variables.
420+
/// </summary>
421+
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
422+
/// <returns>A <c>ValueTask</c> representing the asynchronous operation.</returns>
423+
#if NET45 || NET461 || NET471 || NETSTANDARD1_3 || NETSTANDARD2_0
424+
public async Task ResetConnectionAsync(CancellationToken cancellationToken = default)
425+
#else
426+
public async ValueTask ResetConnectionAsync(CancellationToken cancellationToken = default)
427+
#endif
428+
{
429+
var session = Session;
430+
Log.Debug("Session{0} resetting connection", session.Id);
431+
await session.SendAsync(ResetConnectionPayload.Instance, AsyncIOBehavior, cancellationToken).ConfigureAwait(false);
432+
var payload = await session.ReceiveReplyAsync(AsyncIOBehavior, cancellationToken).ConfigureAwait(false);
433+
OkPayload.Create(payload.Span, session.SupportsDeprecateEof, session.SupportsSessionTrack);
434+
}
435+
418436
[AllowNull]
419437
public override string ConnectionString
420438
{

tests/SideBySide/ConnectionTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Data;
3+
using System.Threading.Tasks;
34
using Dapper;
45
using MySql.Data.MySqlClient;
56
using Xunit;
@@ -241,6 +242,31 @@ public void CloneWithCopiesExistingPassword()
241242
connection2.Open();
242243
Assert.Equal(ConnectionState.Open, connection2.State);
243244
}
245+
246+
[Fact]
247+
public async Task ResetConnectionThrowsIfNotOpen()
248+
{
249+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
250+
await Assert.ThrowsAsync<InvalidOperationException>(async () => await connection.ResetConnectionAsync());
251+
}
252+
253+
[SkippableFact(ServerFeatures.ResetConnection)]
254+
public async Task ResetConnectionClearsUserVariables()
255+
{
256+
var csb = AppConfig.CreateConnectionStringBuilder();
257+
csb.AllowUserVariables = true;
258+
using var connection = new MySqlConnection(csb.ConnectionString);
259+
await connection.OpenAsync();
260+
261+
connection.Execute("set @temp_var = 1;");
262+
var tempVar = connection.ExecuteScalar<int?>("select @temp_var;");
263+
Assert.Equal(1, tempVar);
264+
265+
await connection.ResetConnectionAsync();
266+
267+
tempVar = connection.ExecuteScalar<int?>("select @temp_var;");
268+
Assert.Null(tempVar);
269+
}
244270
#endif
245271
}
246272
}

tests/SideBySide/ServerFeatures.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ public enum ServerFeatures
2323
Ed25519 = 0x4000,
2424
UnixDomainSocket = 0x8000,
2525
Tls13 = 0x1_0000,
26+
ResetConnection = 0x2_0000,
2627
}
2728
}

0 commit comments

Comments
 (0)