Skip to content

Commit

Permalink
Small optimalisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulissoft committed Jul 4, 2024
1 parent 21d406b commit e3cc54c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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("<getConnection({})", useOverflow);
log.trace("<getConnection({}, ...)", useOverflow);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ protected boolean getConnectionFailsDueToNoIdleConnections(final Exception ex) {
return (ex instanceof SQLException) && ex.getMessage().matches(REX_CONNECTION_TIMEOUT);
}

protected Connection getConnection(final boolean useOverflow) throws SQLException {
final Connection conn = super.getConnection(useOverflow);
@Override
protected Connection getConnection(final boolean useOverflow,
final SimplePoolDataSourceOracle poolDataSource,
final SimplePoolDataSourceOracle poolDataSourceOverflow) throws SQLException {
final Connection conn = super.getConnection(useOverflow, poolDataSource, poolDataSourceOverflow);

if (useOverflow) {
// The setInvalid method of the ValidConnection interface
Expand Down

0 comments on commit e3cc54c

Please sign in to comment.