Merge pull request #423 from nitincchauhan/dev

removed redundant creationTime. lastAceess is initialized at creation as
pull/424/merge
Brett Wooldridge 10 years ago
commit 350a18e74f

@ -360,7 +360,6 @@ Here is a list of JDBC *DataSource* classes for popular databases:
| Apache Derby | Derby | org.apache.derby.jdbc.ClientDataSource |
| Firebird | Jaybird | org.firebirdsql.pool.FBSimpleDataSource |
| H2 | H2 | org.h2.jdbcx.JdbcDataSource |
| IBM AS400 | IBM | com.ibm.as400.access.AS400JDBCDriver |
| HSQLDB | HSQLDB | org.hsqldb.jdbc.JDBCDataSource |
| IBM AS400 | IBM | com.ibm.as400.access.AS400JDBCDriver |
| IBM DB2 | DB2 | com.ibm.db2.jcc.DB2SimpleDataSource |

@ -21,12 +21,9 @@ import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.zaxxer.hikari.pool.PoolEntry;
import com.zaxxer.hikari.util.ClockSource;
public final class CodaHaleMetricsTracker extends MetricsTracker
{
private static final ClockSource clockSource = ClockSource.INSTANCE;
private final String poolName;
private final Timer connectionObtainTimer;
private final Histogram connectionUsage;
@ -95,7 +92,7 @@ public final class CodaHaleMetricsTracker extends MetricsTracker
@Override
public void recordConnectionUsage(final PoolEntry bagEntry)
{
connectionUsage.update(clockSource.elapsedMillis(bagEntry.lastOpenTime));
connectionUsage.update(bagEntry.getElapsedLastBorrowed());
}
public Timer getConnectionAcquisitionTimer()
@ -122,12 +119,5 @@ public final class CodaHaleMetricsTracker extends MetricsTracker
{
innerContext.stop();
}
/** {@inheritDoc} */
@Override
public void setConnectionLastOpen(final PoolEntry bagEntry, final long now)
{
bagEntry.lastOpenTime = now;
}
}
}

@ -17,7 +17,6 @@
package com.zaxxer.hikari.metrics;
import com.zaxxer.hikari.pool.PoolEntry;
import com.zaxxer.hikari.util.ClockSource;
/**
* This class only supports realtime, not historical metrics.
@ -59,16 +58,5 @@ public class MetricsTracker implements AutoCloseable
{
// do nothing
}
/**
* Set the lastOpenTime on the provided bag entry.
*
* @param bagEntry the bag entry
* @param now the last open timestamp from {@link ClockSource#currentTime()}
*/
public void setConnectionLastOpen(final PoolEntry bagEntry, final long now)
{
// do nothing
}
}
}

@ -67,23 +67,22 @@ public class HikariPool extends PoolBase implements HikariPoolMXBean, IBagStateL
private static final ClockSource clockSource = ClockSource.INSTANCE;
private final long ALIVE_BYPASS_WINDOW_MS = Long.getLong("com.zaxxer.hikari.aliveBypassWindow", TimeUnit.SECONDS.toMillis(1));
private final long HOUSEKEEPING_PERIOD_MS = Long.getLong("com.zaxxer.hikari.housekeeping.periodMs", TimeUnit.SECONDS.toMillis(30));
private static final long ALIVE_BYPASS_WINDOW_MS = Long.getLong("com.zaxxer.hikari.aliveBypassWindow", TimeUnit.SECONDS.toMillis(1));
private static final long HOUSEKEEPING_PERIOD_MS = Long.getLong("com.zaxxer.hikari.housekeeping.periodMs", TimeUnit.SECONDS.toMillis(30));
private static final int POOL_NORMAL = 0;
private static final int POOL_SUSPENDED = 1;
private static final int POOL_SHUTDOWN = 2;
final ConcurrentBag<PoolEntry> connectionBag;
final ScheduledThreadPoolExecutor houseKeepingExecutorService;
private volatile int poolState;
private final AtomicInteger totalConnections;
private final ThreadPoolExecutor addConnectionExecutor;
private final ThreadPoolExecutor closeConnectionExecutor;
private final ScheduledThreadPoolExecutor houseKeepingExecutorService;
private volatile int poolState;
private final ConcurrentBag<PoolEntry> connectionBag;
private final String poolName;
private final ProxyLeakTask leakTask;
private final SuspendResumeLock suspendResumeLock;
@ -99,7 +98,6 @@ public class HikariPool extends PoolBase implements HikariPoolMXBean, IBagStateL
{
super(config);
this.poolName = config.getPoolName();
this.connectionBag = new ConcurrentBag<>(this);
this.totalConnections = new AtomicInteger();
this.suspendResumeLock = config.isAllowPoolSuspension() ? new SuspendResumeLock() : SuspendResumeLock.FAUX_LOCK;
@ -168,12 +166,12 @@ public class HikariPool extends PoolBase implements HikariPoolMXBean, IBagStateL
}
final long now = clockSource.currentTime();
if (poolEntry.evict || (clockSource.elapsedMillis(poolEntry.lastAccess, now) > ALIVE_BYPASS_WINDOW_MS && !isConnectionAlive(poolEntry.connection))) {
if (poolEntry.evict || (clockSource.elapsedMillis(poolEntry.lastAccessed, now) > ALIVE_BYPASS_WINDOW_MS && !isConnectionAlive(poolEntry.connection))) {
closeConnection(poolEntry, "(connection evicted or dead)"); // Throw away the dead connection and try again
timeout = hardTimeout - clockSource.elapsedMillis(startTime, now);
}
else {
metricsContext.setConnectionLastOpen(poolEntry, now);
poolEntry.lastBorrowed = now;
metricsContext.stop();
return poolEntry.createProxyConnection(leakTask.start(poolEntry), now);
}
@ -627,7 +625,7 @@ public class HikariPool extends PoolBase implements HikariPoolMXBean, IBagStateL
if (removable <= 0) {
break;
}
if (clockSource.elapsedMillis(poolEntry.lastAccess, now) > idleTimeout) {
if (clockSource.elapsedMillis(poolEntry.lastAccessed, now) > idleTimeout) {
if (connectionBag.reserve(poolEntry)) {
closeConnection(poolEntry, "(connection passed idleTimeout)");
removable--;

@ -29,8 +29,9 @@ import com.zaxxer.hikari.util.UtilityElf;
abstract class PoolBase
{
protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
private final Logger LOGGER = LoggerFactory.getLogger(PoolBase.class);
protected final HikariConfig config;
protected final String poolName;
protected long connectionTimeout;
private static final String[] RESET_STATES = {"readOnly", "autoCommit", "isolation", "catalog", "netTimeout"};
@ -45,8 +46,6 @@ abstract class PoolBase
private Executor netTimeoutExecutor;
private DataSource dataSource;
// private final HikariPool hikariPool;
private final String poolName;
private final String catalog;
private final boolean isReadOnly;
private final boolean isAutoCommit;
@ -78,7 +77,7 @@ abstract class PoolBase
initializeDataSource();
}
public String getPoolName()
String getPoolName()
{
return poolName;
}
@ -89,7 +88,7 @@ abstract class PoolBase
// JDBC methods
// ***********************************************************************
public void quietlyCloseConnection(final Connection connection, final String closureReason)
void quietlyCloseConnection(final Connection connection, final String closureReason)
{
try {
if (connection == null || connection.isClosed()) {
@ -110,7 +109,7 @@ abstract class PoolBase
}
}
public boolean isConnectionAlive(final Connection connection)
boolean isConnectionAlive(final Connection connection)
{
try {
final long validationTimeout = config.getValidationTimeout();
@ -144,17 +143,16 @@ abstract class PoolBase
}
}
public DataSource getUnwrappedDataSource()
Throwable getLastConnectionFailure()
{
return dataSource;
return lastConnectionFailure.getAndSet(null);
}
public Throwable getLastConnectionFailure()
public DataSource getUnwrappedDataSource()
{
return lastConnectionFailure.getAndSet(null);
return dataSource;
}
// ***********************************************************************
// PoolEntry methods
// ***********************************************************************
@ -164,7 +162,7 @@ abstract class PoolBase
return new PoolEntry(newConnection(), this);
}
public void resetConnectionState(final Connection connection, final ProxyConnection proxyConnection, final int dirtyBits) throws SQLException
void resetConnectionState(final Connection connection, final ProxyConnection proxyConnection, final int dirtyBits) throws SQLException
{
int resetBits = 0;
@ -201,9 +199,15 @@ abstract class PoolBase
}
}
void shutdownNetworkTimeoutExecutor()
{
if (netTimeoutExecutor != null && netTimeoutExecutor instanceof ThreadPoolExecutor) {
((ThreadPoolExecutor) netTimeoutExecutor).shutdownNow();
}
}
// ***********************************************************************
// PoolMediator methods
// JMX methods
// ***********************************************************************
/**
@ -259,13 +263,6 @@ abstract class PoolBase
}
}
public void shutdownNetworkTimeoutExecutor()
{
if (netTimeoutExecutor != null && netTimeoutExecutor instanceof ThreadPoolExecutor) {
((ThreadPoolExecutor) netTimeoutExecutor).shutdownNow();
}
}
// ***********************************************************************
// Private methods
// ***********************************************************************

@ -18,8 +18,6 @@ package com.zaxxer.hikari.pool;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicInteger;
@ -37,16 +35,12 @@ import com.zaxxer.hikari.util.FastList;
*/
public final class PoolEntry implements IConcurrentBagEntry
{
private static final Logger LOGGER;
private static final SimpleDateFormat DATE_FORMAT;
private static final Logger LOGGER = LoggerFactory.getLogger(PoolEntry.class);
public final long creationTime;
public Connection connection;
public long lastAccess;
public volatile long lastOpenTime;
public volatile boolean evict;
Connection connection;
long lastAccessed;
long lastBorrowed;
volatile boolean evict;
private final FastList<Statement> openStatements;
private final PoolBase poolBase;
@ -54,37 +48,30 @@ public final class PoolEntry implements IConcurrentBagEntry
private volatile ScheduledFuture<?> endOfLife;
static
{
LOGGER = LoggerFactory.getLogger(PoolEntry.class);
DATE_FORMAT = new SimpleDateFormat("MMM dd, HH:mm:ss.SSS");
}
PoolEntry(final Connection connection, final PoolBase pool)
{
this.connection = connection;
this.poolBase = pool;
this.creationTime = System.currentTimeMillis();
this.state = new AtomicInteger(STATE_NOT_IN_USE);
this.lastAccess = ClockSource.INSTANCE.currentTime();
this.lastAccessed = ClockSource.INSTANCE.currentTime();
this.openStatements = new FastList<>(Statement.class, 16);
}
/**
* Release this entry back to the pool.
*
* @param lastAccess last access time-stamp
* @param lastAccessed last access time-stamp
*/
public void recycle(final long lastAccess)
void recycle(final long lastAccessed)
{
this.lastAccess = lastAccess;
this.lastAccessed = lastAccessed;
poolBase.releaseConnection(this);
}
/**
* @param endOfLife
*/
public void setFutureEol(final ScheduledFuture<?> endOfLife)
void setFutureEol(final ScheduledFuture<?> endOfLife)
{
this.endOfLife = endOfLife;
}
@ -94,41 +81,42 @@ public final class PoolEntry implements IConcurrentBagEntry
return ProxyFactory.getProxyConnection(this, connection, openStatements, leakTask, now);
}
public void resetConnectionState(final ProxyConnection proxyConnection, final int dirtyBits) throws SQLException
void resetConnectionState(final ProxyConnection proxyConnection, final int dirtyBits) throws SQLException
{
poolBase.resetConnectionState(connection, proxyConnection, dirtyBits);
}
public String getPoolName()
String getPoolName()
{
return poolBase.getPoolName();
}
public Connection getConnection()
Connection getConnection()
{
return connection;
}
public long getLastAccess()
{
return lastAccess;
}
public boolean isEvicted()
boolean isEvicted()
{
return evict;
}
public void evict()
void evict()
{
this.evict = true;
}
public FastList<Statement> getStatementsList()
FastList<Statement> getStatementsList()
{
return openStatements;
}
/** Returns millis since lastBorrowed */
public long getElapsedLastBorrowed()
{
return ClockSource.INSTANCE.elapsedMillis(lastBorrowed);
}
// ***********************************************************************
// IConcurrentBagEntry methods
// ***********************************************************************
@ -158,9 +146,10 @@ public final class PoolEntry implements IConcurrentBagEntry
@Override
public String toString()
{
final long now = ClockSource.INSTANCE.currentTime();
return connection
+ ", created " + formatDateTime(creationTime)
+ ", last release " + ClockSource.INSTANCE.elapsedMillis(lastAccess) + "ms ago, "
+ ", borrowed " + ClockSource.INSTANCE.elapsedMillis(lastBorrowed, now) + "ms ago, "
+ ", accessed " + ClockSource.INSTANCE.elapsedMillis(lastAccessed, now) + "ms ago, "
+ stateToString();
}
@ -174,11 +163,6 @@ public final class PoolEntry implements IConcurrentBagEntry
connection = null;
}
private static synchronized String formatDateTime(final long timestamp)
{
return DATE_FORMAT.format(new Date(timestamp));
}
private String stateToString()
{
switch (state.get()) {

@ -96,30 +96,30 @@ public abstract class ProxyConnection implements Connection
}
// ***********************************************************************
// Live Connection State accessors
// Connection State Accessors
// ***********************************************************************
public final boolean getAutoCommitState()
final boolean getAutoCommitState()
{
return isAutoCommit;
}
public final String getCatalogState()
final String getCatalogState()
{
return dbcatalog;
}
public final int getTransactionIsolationState()
final int getTransactionIsolationState()
{
return transactionIsolation;
}
public final boolean getReadOnlyState()
final boolean getReadOnlyState()
{
return isReadOnly;
}
public final int getNetworkTimeoutState()
final int getNetworkTimeoutState()
{
return networkTimeout;
}
@ -129,13 +129,13 @@ public abstract class ProxyConnection implements Connection
// ***********************************************************************
/** {@inheritDoc} */
public final PoolEntry getPoolEntry()
final PoolEntry getPoolEntry()
{
return poolEntry;
}
/** {@inheritDoc} */
public final SQLException checkException(final SQLException sqle)
final SQLException checkException(final SQLException sqle)
{
String sqlState = sqle.getSQLState();
if (sqlState != null) {
@ -156,13 +156,13 @@ public abstract class ProxyConnection implements Connection
}
/** {@inheritDoc} */
public final void untrackStatement(final Statement statement)
final void untrackStatement(final Statement statement)
{
openStatements.remove(statement);
}
/** {@inheritDoc} */
public final void markCommitStateDirty()
final void markCommitStateDirty()
{
if (isAutoCommit) {
lastAccess = clockSource.currentTime();

@ -87,7 +87,7 @@ class ProxyLeakTask implements Runnable
LOGGER.warn("Connection leak detection triggered for connection {}, stack trace follows", connectionName, exception);
}
public void cancel()
void cancel()
{
scheduledFuture.cancel(false);
}

@ -39,7 +39,7 @@ public abstract class ProxyResultSet implements ResultSet
this.delegate = resultSet;
}
protected final SQLException checkException(SQLException e)
final SQLException checkException(SQLException e)
{
return connection.checkException(e);
}

@ -40,7 +40,7 @@ public abstract class ProxyStatement implements Statement
this.delegate = statement;
}
protected final SQLException checkException(SQLException e)
final SQLException checkException(SQLException e)
{
return connection.checkException(e);
}

@ -244,9 +244,9 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
public List<T> values(final int state)
{
final ArrayList<T> list = new ArrayList<>(sharedList.size());
for (final T reference : sharedList) {
if (reference.getState() == state) {
list.add(reference);
for (final T entry : sharedList) {
if (entry.getState() == state) {
list.add(entry);
}
}
@ -320,8 +320,8 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
public int getCount(final int state)
{
int count = 0;
for (final T reference : sharedList) {
if (reference.getState() == state) {
for (final T entry : sharedList) {
if (entry.getState() == state) {
count++;
}
}

@ -291,7 +291,7 @@ public class ShutdownTest
Assert.fail(e.getMessage());
}
finally {
TestElf.getPool(ds).quietlyCloseConnection(connection, "(because this is a test)");
try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
ds.close();
}
};

Loading…
Cancel
Save