Example #1
0
  public PoolBagEntry(final Connection connection, final HikariPool pool) {
    this.connection = connection;
    this.parentPool = pool;
    this.creationTime = System.currentTimeMillis();
    this.poolElf = pool.poolElf;
    this.state = new AtomicInteger(STATE_NOT_IN_USE);
    this.lastAccess = ClockSource.INSTANCE.currentTime();
    this.openStatements = new FastList<>(Statement.class, 16);

    poolElf.resetPoolEntry(this);

    final long maxLifetime = pool.config.getMaxLifetime();
    final long variance = maxLifetime > 60_000 ? ThreadLocalRandom.current().nextLong(10_000) : 0;
    final long lifetime = maxLifetime - variance;
    if (lifetime > 0) {
      endOfLife =
          pool.houseKeepingExecutorService.schedule(
              new Runnable() {
                @Override
                public void run() {
                  // If we can reserve it, close it
                  if (pool.connectionBag.reserve(PoolBagEntry.this)) {
                    pool.closeConnection(PoolBagEntry.this, "(connection reached maxLifetime)");
                  } else {
                    // else the connection is "in-use" and we mark it for eviction by
                    // pool.releaseConnection() or the housekeeper
                    PoolBagEntry.this.evicted = true;
                  }
                }
              },
              lifetime,
              TimeUnit.MILLISECONDS);
    }
  }
Example #2
0
 /**
  * Reset the connection to its original state.
  *
  * @throws SQLException thrown if there is an error resetting the connection state
  */
 public void resetConnectionState() throws SQLException {
   poolElf.resetConnectionState(this);
   poolElf.resetPoolEntry(this);
 }