BoneCP implements Serializable, Closeable { public boolean isConnectionHandleAlive(ConnectionHandle connection) { Statement stmt = null; boolean result = false; boolean logicallyClosed = connection.logicallyClosed.get(); try { connection.logicallyClosed.compareAndSet(true, false); String testStatement = this.config.getConnectionTestStatement(); ResultSet rs = null; if (testStatement == null) { rs = connection.getMetaData().getTables( null, null, KEEPALIVEMETADATA, METADATATABLE ); } else { stmt = connection.createStatement(); stmt.execute(testStatement); } if (rs != null) { rs.close(); } result = true; } catch (SQLException e) { result = false; } finally { connection.logicallyClosed.set(logicallyClosed); connection.setConnectionLastResetInMs(System.currentTimeMillis()); result = closeStatement(stmt, result); } return result; } 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 testIsConnectionHandleAlive() throws SQLException { expect(mockConfig.getConnectionTestStatement()).andReturn(null).once(); mockConnection.logicallyClosed=new AtomicBoolean(); expect(mockConnection.getMetaData()).andReturn(mockDatabaseMetadata).once(); expect(mockDatabaseMetadata.getTables((String)anyObject(), (String)anyObject(), (String)anyObject(), (String[])anyObject())).andReturn(mockResultSet).once(); mockResultSet.close(); expectLastCall().once(); replay(mockConfig, mockConnection, mockDatabaseMetadata, mockResultSet); assertTrue(testClass.isConnectionHandleAlive(mockConnection)); verify(mockConfig, mockConnection, mockResultSet,mockDatabaseMetadata); }
@Test public void testIsConnectionHandleAliveTriggerException() throws SQLException { reset(mockConfig, mockConnection, mockDatabaseMetadata, mockResultSet); expect(mockConfig.getConnectionTestStatement()).andReturn(null).once(); expect(mockConnection.getMetaData()).andThrow(new SQLException()).once(); mockConnection.logicallyClosed = new AtomicBoolean(false); replay(mockConfig, mockConnection, mockDatabaseMetadata, mockResultSet); assertFalse(testClass.isConnectionHandleAlive(mockConnection)); verify(mockConfig, mockConnection, mockResultSet,mockDatabaseMetadata); }
@Test public void testIsConnectionHandleAliveNormalCaseWithConnectionTestStatement() throws SQLException { reset(mockConfig, mockConnection, mockDatabaseMetadata, mockResultSet); Statement mockStatement = EasyMock.createNiceMock(Statement.class); expect(mockConfig.getConnectionTestStatement()).andReturn("whatever").once(); expect(mockConnection.createStatement()).andReturn(mockStatement).once(); expect(mockStatement.execute((String)anyObject())).andReturn(true).once(); mockConnection.logicallyClosed = new AtomicBoolean(true); replay(mockConfig, mockConnection, mockDatabaseMetadata,mockStatement); assertTrue(testClass.isConnectionHandleAlive(mockConnection)); verify(mockConfig, mockConnection,mockDatabaseMetadata, mockStatement); }