@@ -40,41 +40,47 @@ public async Task<MySqlSession> GetSessionAsync(MySqlConnection connection, IOBe
4040 }
4141 if ( session != null )
4242 {
43- if ( session . PoolGeneration != m_generation || ! await session . TryPingAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) )
43+ bool reuseSession ;
44+
45+ if ( session . PoolGeneration != m_generation )
4446 {
45- // session is either old or cannot communicate with the server
46- await session . DisposeAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
47+ reuseSession = false ;
4748 }
4849 else
4950 {
50- bool resetFailed = false ;
51-
52- // session is valid, reset if supported
5351 if ( m_connectionSettings . ConnectionReset )
5452 {
5553 try
5654 {
5755 // Depending on the server (Aurora?), this can randomly fail when re-authenticating to the server.
5856 // If it does, we can just pretend it didn't happen and continue with creating a new session below.
5957 await session . ResetConnectionAsync ( m_connectionSettings , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
58+ reuseSession = true ;
6059 }
61- catch
60+ catch ( Exception )
6261 {
63- resetFailed = true ;
64-
65- await session . DisposeAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
62+ reuseSession = false ;
6663 }
6764 }
68-
69- if ( ! resetFailed )
65+ else
7066 {
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 ;
67+ reuseSession = await session . TryPingAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
7668 }
7769 }
70+
71+ if ( ! reuseSession )
72+ {
73+ // session is either old or cannot communicate with the server
74+ await session . DisposeAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
75+ }
76+ else
77+ {
78+ // pooled session is ready to be used; return it
79+ session . OwningConnection = new WeakReference < MySqlConnection > ( connection ) ;
80+ lock ( m_leasedSessions )
81+ m_leasedSessions . Add ( session . Id , session ) ;
82+ return session ;
83+ }
7884 }
7985
8086 // create a new session
0 commit comments