Skip to content

Commit 0eb9ddb

Browse files
committed
Allow sessions with changed databases to be pooled. Fixes #515
1 parent af35129 commit 0eb9ddb

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public async ValueTask<ServerSession> GetSessionAsync(MySqlConnection connection
6767
}
6868
else
6969
{
70-
if (ConnectionSettings.ConnectionReset)
70+
if (ConnectionSettings.ConnectionReset || session.DatabaseOverride != null)
7171
{
7272
reuseSession = await session.TryResetConnectionAsync(ConnectionSettings, ioBehavior, cancellationToken).ConfigureAwait(false);
7373
}
@@ -148,10 +148,8 @@ private bool SessionIsHealthy(ServerSession session)
148148
return false;
149149
if (session.PoolGeneration != m_generation)
150150
return false;
151-
if (session.DatabaseOverride != null)
152-
return false;
153151
if (ConnectionSettings.ConnectionLifeTime > 0
154-
&& unchecked((uint) Environment.TickCount) - session.CreatedTicks >= ConnectionSettings.ConnectionLifeTime)
152+
&& unchecked((uint) Environment.TickCount) - session.CreatedTicks >= ConnectionSettings.ConnectionLifeTime)
155153
return false;
156154

157155
return true;

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
364364

365365
try
366366
{
367-
if (ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0)
367+
if (DatabaseOverride == null && ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0)
368368
{
369369
m_logArguments[1] = ServerVersion.OriginalString;
370370
Log.Debug("Session{0} ServerVersion={1} supports reset connection; sending reset connection request", m_logArguments);
@@ -380,8 +380,17 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
380380
else
381381
{
382382
// optimistically hash the password with the challenge from the initial handshake (supported by MariaDB; doesn't appear to be supported by MySQL)
383-
m_logArguments[1] = ServerVersion.OriginalString;
384-
Log.Debug("Session{0} ServerVersion={1} doesn't support reset connection; sending change user request", m_logArguments);
383+
if (DatabaseOverride == null)
384+
{
385+
m_logArguments[1] = ServerVersion.OriginalString;
386+
Log.Debug("Session{0} ServerVersion={1} doesn't support reset connection; sending change user request", m_logArguments);
387+
}
388+
else
389+
{
390+
m_logArguments[1] = DatabaseOverride;
391+
Log.Debug("Session{0} sending change user request due to changed Database={1}", m_logArguments);
392+
DatabaseOverride = null;
393+
}
385394
var hashedPassword = AuthenticationUtility.CreateAuthenticationResponse(AuthPluginData, 0, cs.Password);
386395
using (var changeUserPayload = ChangeUserPayload.Create(cs.UserID, hashedPassword, cs.Database, m_supportsConnectionAttributes ? s_connectionAttributes : null))
387396
await SendAsync(changeUserPayload, ioBehavior, cancellationToken).ConfigureAwait(false);

0 commit comments

Comments
 (0)