/** Closes off all connections in all partitions. */ public void terminateAllConnections(){ this.terminationLock.lock(); try{ // close off all connections. for (int i=0; i < this.pool.partitionCount; i++) { this.pool.partitions[i].setUnableToCreateMoreTransactions(false); // we can create new ones now, this is an optimization List<ConnectionHandle> clist = new LinkedList<ConnectionHandle>(); this.pool.partitions[i].getFreeConnections().drainTo(clist); for (ConnectionHandle c: clist){ this.pool.destroyConnection(c); } } } finally { this.terminationLock.unlock(); } }
@Override public ConnectionHandle pollConnection(){ ConnectionHandle result = null; int partition = (int) (Thread.currentThread().getId() % this.pool.partitionCount); ConnectionPartition connectionPartition = this.pool.partitions[partition]; result = connectionPartition.getFreeConnections().poll(); if (result == null) { // we ran out of space on this partition, pick another free one for (int i=0; i < this.pool.partitionCount; i++){ if (i == partition) { continue; // we already determined it's not here } result = this.pool.partitions[i].getFreeConnections().poll(); // try our luck with this partition if (result != null) { connectionPartition = this.pool.partitions[i]; // we found it here break; // we found a connection } } } if (!connectionPartition.isUnableToCreateMoreTransactions()){ // unless we can't create any more connections... this.pool.maybeSignalForMoreConnections(connectionPartition); // see if we need to create more } return result; }
@Override protected Connection getConnectionInternal() throws SQLException { ConnectionHandle result = pollConnection(); // we still didn't find an empty one, wait forever (or as per config) until our partition is free if (result == null) { int partition = (int) (Thread.currentThread().getId() % this.pool.partitionCount); ConnectionPartition connectionPartition = this.pool.partitions[partition]; try { result = connectionPartition.getFreeConnections().poll(this.pool.connectionTimeoutInMs, TimeUnit.MILLISECONDS); if (result == null){ if (this.pool.nullOnConnectionTimeout){ return null; } // 08001 = The application requester is unable to establish the connection. throw new SQLException("Timed out waiting for a free available connection.", "08001"); } } catch (InterruptedException e) { if (this.pool.nullOnConnectionTimeout){ return null; } throw PoolUtil.generateSQLException(e.getMessage(), e); } } return result; }
/** Places a connection back in the originating partition. * @param connectionHandle to place back * @throws SQLException on error */ protected void putConnectionBackInPartition(ConnectionHandle connectionHandle) throws SQLException { if (this.cachedPoolStrategy && ((CachedConnectionStrategy)this.connectionStrategy).tlConnections.dumbGet().getValue()){ connectionHandle.logicallyClosed.set(true); ((CachedConnectionStrategy)this.connectionStrategy).tlConnections.set(new AbstractMap.SimpleEntry<ConnectionHandle, Boolean>(connectionHandle, false)); } else { BlockingQueue<ConnectionHandle> queue = connectionHandle.getOriginatingPartition().getFreeConnections(); if (!queue.offer(connectionHandle)){ // this shouldn't fail connectionHandle.internalClose(); } } }
/** * Places a connection back in the originating partition. * * @param connectionHandle to place back * @throws SQLException on error */ protected void putConnectionBackInPartition(ConnectionHandle connectionHandle) throws SQLException { if (this.cachedPoolStrategy && ((CachedConnectionStrategy) this.connectionStrategy).tlConnections.dumbGet().getValue()) { connectionHandle.logicallyClosed.set(true); ((CachedConnectionStrategy) this.connectionStrategy).tlConnections.set(new AbstractMap.SimpleEntry<ConnectionHandle, Boolean>(connectionHandle, false)); } else { BlockingQueue<ConnectionHandle> queue = connectionHandle.getOriginatingPartition().getFreeConnections(); if (!queue.offer(connectionHandle)) { // this shouldn't fail connectionHandle.internalClose(); } } }
for (int i=0; i < partitionSize; i++){ try { connection = this.partition.getFreeConnections().poll(); if (!(connection.getOriginatingPartition().getFreeConnections().offer(connection))){ connection.internalClose();
connection = this.partition.getFreeConnections().poll(); if (connection != null){ connection.setOriginatingPartition(this.partition); if (!(connection.getOriginatingPartition().getFreeConnections().offer(connection))){ connection.internalClose();
@Override public String toString() { return Objects.toStringHelper(this) .add("url", this.pool.getConfig().getJdbcUrl()) .add("user", this.pool.getConfig().getUsername()) .add("minConnections", this.getMinConnections()) .add("maxConnections", this.getMaxConnections()) .add("acquireIncrement", this.acquireIncrement) .add("createdConnections", this.createdConnections) .add("freeConnections", this.getFreeConnections()) .toString(); } }