From e3cc54c9963d4d8d21aa7a4f67336337a6a23f18 Mon Sep 17 00:00:00 2001 From: Gert-Jan Paulissen Date: Thu, 4 Jul 2024 09:02:53 +0200 Subject: [PATCH] Small optimalisation. --- .../pato/jdbc/SmartPoolDataSource.java | 27 ++++++++++++------- .../pato/jdbc/SmartPoolDataSourceOracle.java | 7 +++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/jdbc/smart-pool-data-source/src/main/java/com/paulissoft/pato/jdbc/SmartPoolDataSource.java b/jdbc/smart-pool-data-source/src/main/java/com/paulissoft/pato/jdbc/SmartPoolDataSource.java index 8c133afe..9ff2ab70 100644 --- a/jdbc/smart-pool-data-source/src/main/java/com/paulissoft/pato/jdbc/SmartPoolDataSource.java +++ b/jdbc/smart-pool-data-source/src/main/java/com/paulissoft/pato/jdbc/SmartPoolDataSource.java @@ -361,20 +361,27 @@ public final Connection getConnection() throws SQLException { state.toString())); } + // minimize accessing volatile variables by shadowing them + final T poolDataSourceOverflow = this.poolDataSourceOverflow; + Connection conn; // Try to avoid connection timeout errors. - // When the pool data source is full with idle connections, use immediately the dynamic pool (and try the fixed one later on). - if (hasOverflow() && poolDataSource.getIdleConnections() == 0 && poolDataSource.getTotalConnections() == poolDataSource.getMaxPoolSize()) { + // When the fixed pool data source is full with idle connections, + // use immediately the dynamic pool (and try the fixed one later on). + if (poolDataSourceOverflow != null && + poolDataSource.getIdleConnections() == 0 && + poolDataSource.getTotalConnections() == poolDataSource.getMaxPoolSize()) { useOverflow = true; } try { - conn = getConnection(useOverflow); + conn = getConnection(useOverflow, poolDataSource, poolDataSourceOverflow); } catch (Exception ex) { // switch pools (just once) when the connection fails due to no idle connections - if ((useOverflow || hasOverflow()) && getConnectionFailsDueToNoIdleConnections(ex)) { - conn = getConnection(!useOverflow); + if ((useOverflow || poolDataSourceOverflow != null) && + getConnectionFailsDueToNoIdleConnections(ex)) { + conn = getConnection(!useOverflow, poolDataSource, poolDataSourceOverflow); } else { throw ex; } @@ -386,16 +393,18 @@ public final Connection getConnection() throws SQLException { } } - protected Connection getConnection(final boolean useOverflow) throws SQLException { - log.trace(">getConnection({})", useOverflow); + protected Connection getConnection(final boolean useOverflow, + final T poolDataSource, + final T poolDataSourceOverflow) throws SQLException { + log.trace(">getConnection({}, ...)", useOverflow); - final T pds = useOverflow ? getPoolDataSourceOverflow() : getPoolDataSource(); + final T pds = useOverflow ? poolDataSourceOverflow : poolDataSource; final PoolDataSourceStatistics pdsStatistics = useOverflow ? getPoolDataSourceStatisticsOverflow() : getPoolDataSourceStatistics(); try { return getConnection(pds, pdsStatistics, hasShownConfig[useOverflow ? 1 : 0]); } finally { - log.trace("