BoneCP implements Serializable, Closeable { protected void internalReleaseConnection(ConnectionHandle connectionHandle) throws SQLException { if (!this.cachedPoolStrategy){ connectionHandle.clearStatementCaches(false); } if (connectionHandle.getReplayLog() != null){ connectionHandle.getReplayLog().clear(); connectionHandle.recoveryResult.getReplaceTarget().clear(); } if (connectionHandle.isExpired() || (!this.poolShuttingDown && connectionHandle.isPossiblyBroken() && !isConnectionHandleAlive(connectionHandle))){ if (connectionHandle.isExpired()) { connectionHandle.internalClose(); } ConnectionPartition connectionPartition = connectionHandle.getOriginatingPartition(); postDestroyConnection(connectionHandle); maybeSignalForMoreConnections(connectionPartition); connectionHandle.clearStatementCaches(true); return; } connectionHandle.setConnectionLastUsedInMs(System.currentTimeMillis()); if (!this.poolShuttingDown){ putConnectionBackInPartition(connectionHandle); } else { connectionHandle.internalClose(); } } BoneCP(BoneCPConfig config); synchronized void shutdown(); void close(); Connection getConnection(); ListenableFuture<Connection> getAsyncConnection(); boolean isConnectionHandleAlive(ConnectionHandle connection); int getTotalLeased(); int getTotalFree(); int getTotalCreatedConnections(); BoneCPConfig getConfig(); Statistics getStatistics(); AtomicBoolean getDbIsDown(); static final String MBEAN_CONFIG; static final String MBEAN_BONECP; }
@Test public void testInternalReleaseConnection() throws InterruptedException, SQLException { mockConnection.setConnectionLastUsedInMs(anyLong()); expectLastCall().once(); expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockPartition.getAvailableConnections()).andReturn(1).anyTimes(); Connection mockRealConnection = EasyMock.createNiceMock(Connection.class); expect(mockConnection.getInternalConnection()).andReturn(mockRealConnection).anyTimes(); expect(mockConnectionHandles.offer(mockConnection)).andReturn(true).once(); replay(mockRealConnection, mockConnection,mockPartition, mockConnectionHandles); testClass.internalReleaseConnection(mockConnection); verify(mockConnection,mockPartition, mockConnectionHandles); }
@Test public void testInternalReleaseConnectionWhereConnectionIsBroken() throws InterruptedException, SQLException { mockConnection.logicallyClosed = new AtomicBoolean(false); reset(mockConnection,mockPartition, mockConnectionHandles); expect(mockConnection.isPossiblyBroken()).andReturn(true).once(); expect(mockConfig.getConnectionTestStatement()).andReturn(null).once(); expect(mockConnection.getMetaData()).andThrow(new SQLException()).once(); expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes(); mockPartition.setUnableToCreateMoreTransactions(false); expectLastCall().once(); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); Connection mockRealConnection = EasyMock.createNiceMock(Connection.class); expect(mockConnection.getInternalConnection()).andReturn(mockRealConnection).anyTimes(); replay(mockPartition, mockConnection); testClass.internalReleaseConnection(mockConnection); verify(mockPartition, mockConnection); }
@Test public void testInternalReleaseConnectionWhereConnectionIsMarkedBrokenButConnectionTestSucceeds() throws InterruptedException, SQLException { mockConnection.logicallyClosed = new AtomicBoolean(false); reset(mockConnection,mockPartition, mockConnectionHandles); expect(mockConnection.isPossiblyBroken()).andReturn(true).once(); expect(mockConfig.getConnectionTestStatement()).andReturn(null).once(); expect(mockConnection.getMetaData()).andReturn(EasyMock.createNiceMock(DatabaseMetaData.class)).once(); expect(mockPartition.getFreeConnections()).andReturn(mockConnectionHandles).anyTimes(); expect(mockPartition.getAvailableConnections()).andReturn(1).anyTimes(); expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes(); Connection mockRealConnection = EasyMock.createNiceMock(Connection.class); expect(mockConnection.getInternalConnection()).andReturn(mockRealConnection).anyTimes(); replay(mockPartition, mockConnection); testClass.internalReleaseConnection(mockConnection); verify(mockPartition, mockConnection); }
@Test public void testInternalReleaseConnectionWherePoolisShuttingDown() throws InterruptedException, SQLException { mockConnection.logicallyClosed = new AtomicBoolean(false); reset(mockConnection,mockPartition, mockConnectionHandles); expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes(); Connection mockRealConnection = EasyMock.createNiceMock(Connection.class); expect(mockConnection.getInternalConnection()).andReturn(mockRealConnection).anyTimes(); mockConnection.internalClose(); expectLastCall().once(); replay(mockPartition, mockConnection); testClass.poolShuttingDown = true; testClass.internalReleaseConnection(mockConnection); testClass.poolShuttingDown = false; verify(mockPartition, mockConnection); }
@Test public void testInternalReleaseConnectionWhereConnectionIsExpired() throws InterruptedException, SQLException { reset(mockConnection, mockPartition, mockConnectionHandles); Connection mockRealConnection = EasyMock.createNiceMock(Connection.class); expect(mockConnection.getInternalConnection()).andReturn(mockRealConnection).anyTimes(); expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition).anyTimes(); expect(mockPartition.isUnableToCreateMoreTransactions()).andReturn(true).once(); expect(mockConnection.isExpired()).andReturn(true).anyTimes(); mockConnection.internalClose(); expectLastCall(); replay(mockPartition, mockConnection, mockRealConnection); testClass.internalReleaseConnection(mockConnection); verify(mockPartition, mockConnection, mockRealConnection); }