@@ -474,25 +474,30 @@ private java.sql.Connection getRecycledConnection() throws SQLException {
474474
475475 PooledConnection pconn = wrapper .connection ;
476476
477- // Smart validation: skip validation for recently used connections
478- boolean needsValidation = wrapper .isExpired (connectionMaxAgeMs ) ||
479- wrapper .isIdle (connectionIdleTimeoutMs ) ||
480- !isRecentlyValidated (pconn );
481-
482- if (needsValidation && !validateConnection (pconn )) {
483- // Connection is invalid, dispose it properly
484- doPurgeConnection .set (true );
485- try {
486- pconn .removeConnectionEventListener (poolConnectionEventListener );
487- pconn .close ();
488- } catch (SQLException e ) {
489- // Ignore close errors for invalid connections
490- } finally {
491- doPurgeConnection .set (false );
477+ // Skip all validation for very recently recycled connections (< 5 seconds)
478+ // This dramatically improves performance for high-frequency connection reuse scenarios
479+ boolean validateConnection = (System .currentTimeMillis () - wrapper .lastUsedTime ) > 5000 ;
480+ if (validateConnection ) {
481+ // Standard validation path for older connections
482+ boolean needsValidation = wrapper .isExpired (connectionMaxAgeMs ) ||
483+ wrapper .isIdle (connectionIdleTimeoutMs ) ||
484+ !isRecentlyValidated (pconn );
485+
486+ if (needsValidation && !validateConnection (pconn )) {
487+ // Connection is invalid, dispose it properly
488+ doPurgeConnection .set (true );
489+ try {
490+ pconn .removeConnectionEventListener (poolConnectionEventListener );
491+ pconn .close ();
492+ } catch (SQLException e ) {
493+ // Ignore close errors for invalid connections
494+ } finally {
495+ doPurgeConnection .set (false );
496+ }
497+ connectionWrappers .remove (pconn );
498+ totalConnections .decrementAndGet ();
499+ return null ; // Try another recycled connection or create new one
492500 }
493- connectionWrappers .remove (pconn );
494- totalConnections .decrementAndGet ();
495- return null ; // Try another recycled connection or create new one
496501 }
497502
498503 // Connection is valid, update its usage time and return it
0 commit comments