Skip to content

Commit c683cb2

Browse files
committed
Implement IDisposable on ConnectionPool.
This allows all background work to be cancelled when a pool is no longer needed; the primary use case is cleanly shutting down a connection pool represented by a MySqlDataSource.
1 parent c33f89c commit c683cb2

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
namespace MySqlConnector.Core;
1010

11-
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
12-
internal sealed class ConnectionPool
13-
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
11+
internal sealed class ConnectionPool : IDisposable
1412
{
1513
public int Id { get; }
1614

@@ -223,6 +221,30 @@ public async Task ReapAsync(IOBehavior ioBehavior, CancellationToken cancellatio
223221
return procedureCache;
224222
}
225223

224+
public void Dispose()
225+
{
226+
Log.Debug("Pool{0} disposing connection pool", m_logArguments);
227+
#if NET6_0_OR_GREATER
228+
m_dnsCheckTimer?.Dispose();
229+
m_dnsCheckTimer = null;
230+
m_reaperTimer?.Dispose();
231+
m_reaperTimer = null;
232+
#else
233+
if (m_dnsCheckTimer is not null)
234+
{
235+
using var dnsCheckWaitHandle = new ManualResetEvent(false);
236+
m_dnsCheckTimer.Dispose(dnsCheckWaitHandle);
237+
dnsCheckWaitHandle.WaitOne();
238+
}
239+
if (m_reaperTimer is not null)
240+
{
241+
using var reaperWaitHandle = new ManualResetEvent(false);
242+
m_reaperTimer.Dispose(reaperWaitHandle);
243+
reaperWaitHandle.WaitOne();
244+
}
245+
#endif
246+
}
247+
226248
/// <summary>
227249
/// Examines all the <see cref="ServerSession"/> objects in <see cref="m_leasedSessions"/> to determine if any
228250
/// have an owning <see cref="MySqlConnection"/> that has been garbage-collected. If so, assumes that the connection

src/MySqlConnector/MySqlDataSource.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ protected override void Dispose(bool disposing)
9191
private async ValueTask DisposeAsync(IOBehavior ioBehavior)
9292
{
9393
if (Pool is not null)
94+
{
9495
await Pool.ClearAsync(ioBehavior, default).ConfigureAwait(false);
96+
Pool.Dispose();
97+
}
9598
m_isDisposed = true;
9699
}
97100

0 commit comments

Comments
 (0)