Skip to content

Commit 79bd53b

Browse files
committed
Try/catch around ResetConnectionAsync, fall back to creating a new session (works around mysql-net#208)
1 parent 3129812 commit 79bd53b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/MySqlConnector/MySqlClient/ConnectionPool.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,33 @@ public async Task<MySqlSession> GetSessionAsync(MySqlConnection connection, IOBe
4747
}
4848
else
4949
{
50+
bool resetFailed = false;
51+
5052
// session is valid, reset if supported
5153
if (m_connectionSettings.ConnectionReset)
5254
{
53-
await session.ResetConnectionAsync(m_connectionSettings, ioBehavior, cancellationToken).ConfigureAwait(false);
55+
try
56+
{
57+
// Depending on the server (Aurora?), this can randomly fail when re-authenticating to the server.
58+
// If it does, we can just pretend it didn't happen and continue with creating a new session below.
59+
await session.ResetConnectionAsync(m_connectionSettings, ioBehavior, cancellationToken).ConfigureAwait(false);
60+
}
61+
catch
62+
{
63+
resetFailed = true;
64+
65+
await session.DisposeAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
66+
}
5467
}
5568

56-
// pooled session is ready to be used; return it
57-
session.OwningConnection = new WeakReference<MySqlConnection>(connection);
58-
lock (m_leasedSessions)
59-
m_leasedSessions.Add(session.Id, session);
60-
return session;
69+
if (!resetFailed)
70+
{
71+
// pooled session is ready to be used; return it
72+
session.OwningConnection = new WeakReference<MySqlConnection>(connection);
73+
lock (m_leasedSessions)
74+
m_leasedSessions.Add(session.Id, session);
75+
return session;
76+
}
6177
}
6278
}
6379

0 commit comments

Comments
 (0)