@@ -77,10 +77,7 @@ class MysqlConnectionPool {
7777 entry.idleTimer? .cancel ();
7878 entry.idleTimer = null ;
7979 entry.refs++ ;
80- if (! entry.connection.isConnected) {
81- await entry.connection.connect ();
82- await entry.connection.setSessionReadOnly (mode.isReadOnlySession);
83- }
80+ if (! entry.connection.isConnected) await _reconnect (entry, mode);
8481 return MysqlLease ._(this , entry, entry.connection);
8582 }
8683
@@ -113,10 +110,7 @@ class MysqlConnectionPool {
113110 entry.idleTimer? .cancel ();
114111 entry.idleTimer = null ;
115112 entry.refs++ ;
116- if (! entry.connection.isConnected) {
117- await entry.connection.connect ();
118- await entry.connection.setSessionReadOnly (mode.isReadOnlySession);
119- }
113+ if (! entry.connection.isConnected) await _reconnect (entry, mode);
120114 return MysqlLease ._(this , entry, entry.connection);
121115 }
122116
@@ -143,6 +137,21 @@ class MysqlConnectionPool {
143137 /// Releases one lease on [entry] . A lease taken before an [interrupt] points
144138 /// at an entry that is no longer in the pool: it must not touch the entry
145139 /// that replaced it under the same key, so it does nothing.
140+ /// Brings a dropped entry back with one reconnect and one session setting,
141+ /// even when several callers ask at once; they all wait for the same attempt.
142+ Future <void > _reconnect (_PoolEntry entry, MysqlSessionMode mode) {
143+ final inFlight = entry.reconnecting;
144+ if (inFlight != null ) return inFlight;
145+ final attempt = () async {
146+ await entry.connection.connect ();
147+ await entry.connection.setSessionReadOnly (mode.isReadOnlySession);
148+ }();
149+ entry.reconnecting = attempt;
150+ return attempt.whenComplete (() {
151+ if (identical (entry.reconnecting, attempt)) entry.reconnecting = null ;
152+ });
153+ }
154+
146155 void _release (_PoolEntry entry) {
147156 if (! identical (_pool[entry.key], entry)) return ;
148157 entry.refs-- ;
@@ -203,6 +212,7 @@ class _PoolEntry {
203212
204213 final MysqlConnection connection;
205214 final String key;
215+ Future <void >? reconnecting;
206216 int refs = 0 ;
207217 Timer ? idleTimer;
208218 DateTime lastUsed;
0 commit comments