Skip to content

Commit

Permalink
Simplify pool entry last-accessed time handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettwooldridge committed Aug 4, 2021
1 parent 8f254ae commit aa06bb8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/zaxxer/hikari/pool/HikariPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public Connection getConnection(final long hardTimeout) throws SQLException
}
else {
metricsTracker.recordBorrowStats(poolEntry, startTime);
return poolEntry.createProxyConnection(leakTaskFactory.schedule(poolEntry), now);
return poolEntry.createProxyConnection(leakTaskFactory.schedule(poolEntry));
}
} while (timeout > 0L);

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/zaxxer/hikari/pool/PoolEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ final class PoolEntry implements IConcurrentBagEntry
this.hikariPool = (HikariPool) pool;
this.isReadOnly = isReadOnly;
this.isAutoCommit = isAutoCommit;
this.lastAccessed = currentTime();
this.lastAccessed = System.currentTimeMillis();
this.openStatements = new FastList<>(Statement.class, 16);
}

/**
* Release this entry back to the pool.
*
* @param lastAccessed last access time-stamp
*/
void recycle(final long lastAccessed)
void recycle()
{
if (connection != null) {
this.lastAccessed = lastAccessed;
this.lastAccessed = System.currentTimeMillis();
hikariPool.recycle(this);
}
}
Expand All @@ -97,9 +95,9 @@ public void setKeepalive(ScheduledFuture<?> keepalive) {
this.keepalive = keepalive;
}

Connection createProxyConnection(final ProxyLeakTask leakTask, final long now)
Connection createProxyConnection(final ProxyLeakTask leakTask)
{
return ProxyFactory.getProxyConnection(this, connection, openStatements, leakTask, now, isReadOnly, isAutoCommit);
return ProxyFactory.getProxyConnection(this, connection, openStatements, leakTask, isReadOnly, isAutoCommit);
}

void resetConnectionState(final ProxyConnection proxyConnection, final int dirtyBits) throws SQLException
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
import java.util.concurrent.Executor;

import static com.zaxxer.hikari.SQLExceptionOverride.Override.DO_NOT_EVICT;
import static com.zaxxer.hikari.util.ClockSource.currentTime;

/**
* This is the proxy class for java.sql.Connection.
*
* @author Brett Wooldridge
*/
@SuppressWarnings("ClassEscapesDefinedScope")
public abstract class ProxyConnection implements Connection
{
static final int DIRTY_BIT_READONLY = 0b000001;
Expand All @@ -57,7 +55,6 @@ public abstract class ProxyConnection implements Connection
private final FastList<Statement> openStatements;

private int dirtyBits;
private long lastAccess;
private boolean isCommitStateDirty;

private boolean isReadOnly;
Expand Down Expand Up @@ -89,14 +86,12 @@ protected ProxyConnection(final PoolEntry poolEntry,
final Connection connection,
final FastList<Statement> openStatements,
final ProxyLeakTask leakTask,
final long now,
final boolean isReadOnly,
final boolean isAutoCommit) {
this.poolEntry = poolEntry;
this.delegate = connection;
this.openStatements = openStatements;
this.leakTask = leakTask;
this.lastAccess = now;
this.isReadOnly = isReadOnly;
this.isAutoCommit = isAutoCommit;
}
Expand Down Expand Up @@ -196,10 +191,7 @@ final synchronized void untrackStatement(final Statement statement)

final void markCommitStateDirty()
{
if (isAutoCommit) {
lastAccess = currentTime();
}
else {
if (!isAutoCommit) {
isCommitStateDirty = true;
}
}
Expand Down Expand Up @@ -255,13 +247,11 @@ public final void close() throws SQLException
try {
if (isCommitStateDirty && !isAutoCommit) {
delegate.rollback();
lastAccess = currentTime();
LOGGER.debug("{} - Executed rollback on connection {} due to dirty commit state on close().", poolEntry.getPoolName(), delegate);
}

if (dirtyBits != 0) {
poolEntry.resetConnectionState(this, dirtyBits);
lastAccess = currentTime();
}

delegate.clearWarnings();
Expand All @@ -274,7 +264,7 @@ public final void close() throws SQLException
}
finally {
delegate = ClosedConnection.CLOSED_CONNECTION;
poolEntry.recycle(lastAccess);
poolEntry.recycle();
}
}
}
Expand Down Expand Up @@ -386,7 +376,6 @@ public void commit() throws SQLException
{
delegate.commit();
isCommitStateDirty = false;
lastAccess = currentTime();
}

/** {@inheritDoc} */
Expand All @@ -395,7 +384,6 @@ public void rollback() throws SQLException
{
delegate.rollback();
isCommitStateDirty = false;
lastAccess = currentTime();
}

/** {@inheritDoc} */
Expand All @@ -404,7 +392,6 @@ public void rollback(Savepoint savepoint) throws SQLException
{
delegate.rollback(savepoint);
isCommitStateDirty = false;
lastAccess = currentTime();
}

/** {@inheritDoc} */
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/zaxxer/hikari/pool/ProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ private ProxyFactory()
* @param connection the raw database Connection
* @param openStatements a reusable list to track open Statement instances
* @param leakTask the ProxyLeakTask for this connection
* @param now the current timestamp
* @param isReadOnly the default readOnly state of the connection
* @param isAutoCommit the default autoCommit state of the connection
* @return a proxy that wraps the specified {@link Connection}
*/
static ProxyConnection getProxyConnection(final PoolEntry poolEntry, final Connection connection, final FastList<Statement> openStatements, final ProxyLeakTask leakTask, final long now, final boolean isReadOnly, final boolean isAutoCommit)
static ProxyConnection getProxyConnection(final PoolEntry poolEntry, final Connection connection, final FastList<Statement> openStatements, final ProxyLeakTask leakTask, final boolean isReadOnly, final boolean isAutoCommit)
{
// Body is replaced (injected) by JavassistProxyFactory
throw new IllegalStateException("You need to run the CLI build and you need target/classes in your classpath to run.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void testCodegen() throws Exception {
connection,
fastList,
null /*leakTask*/,
0L /*now*/,
Boolean.FALSE /*isReadOnly*/,
Boolean.FALSE /*isAutoCommit*/);
Assert.assertNotNull(proxyConnection);
Expand Down

0 comments on commit aa06bb8

Please sign in to comment.