Skip to content

Commit 8749627

Browse files
committed
Improve exception message for empty connection pool.
1 parent 2d5189a commit 8749627

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async ValueTask<ServerSession> GetSessionAsync(MySqlConnection connection
2727
// if all sessions are used, see if any have been leaked and can be recovered
2828
// check at most once per second (although this isn't enforced via a mutex so multiple threads might block
2929
// on the lock in RecoverLeakedSessions in high-concurrency situations
30-
if (m_sessionSemaphore.CurrentCount == 0 && unchecked(((uint) Environment.TickCount) - m_lastRecoveryTime) >= 1000u)
30+
if (IsEmpty && unchecked(((uint) Environment.TickCount) - m_lastRecoveryTime) >= 1000u)
3131
{
3232
Log.Info("Pool{0} is empty; recovering leaked sessions", m_logArguments);
3333
RecoverLeakedSessions();
@@ -142,6 +142,12 @@ public async ValueTask<ServerSession> GetSessionAsync(MySqlConnection connection
142142
}
143143
}
144144

145+
/// <summary>
146+
/// Returns <c>true</c> if the connection pool is empty, i.e., all connections are in use. Note that in a highly-multithreaded
147+
/// environment, the value of this property may be stale by the time it's returned.
148+
/// </summary>
149+
internal bool IsEmpty => m_sessionSemaphore.CurrentCount == 0;
150+
145151
// Returns zero for healthy, non-zero otherwise.
146152
private int GetSessionHealth(ServerSession session)
147153
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,8 @@ private async ValueTask<ServerSession> CreateSessionAsync(ConnectionPool? pool,
640640
}
641641
catch (OperationCanceledException ex) when (timeoutSource?.IsCancellationRequested ?? false)
642642
{
643-
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired.", ex);
643+
var messageSuffix = (pool?.IsEmpty ?? false) ? " All pooled connections are in use." : "";
644+
throw new MySqlException(MySqlErrorCode.UnableToConnectToHost, "Connect Timeout expired." + messageSuffix, ex);
644645
}
645646
finally
646647
{

0 commit comments

Comments
 (0)