Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Patch idle connection #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bonecp-jdk-compat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-parent</artifactId>
<version>0.8.1-SNAPSHOT</version>
<version>0.8.0-patchidleconnection-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion bonecp-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-parent</artifactId>
<version>0.8.1-SNAPSHOT</version>
<version>0.8.0-patchidleconnection-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion bonecp-test-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-parent</artifactId>
<version>0.8.1-SNAPSHOT</version>
<version>0.8.0-patchidleconnection-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion bonecp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-parent</artifactId>
<version>0.8.1-SNAPSHOT</version>
<version>0.8.0-patchidleconnection-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
11 changes: 9 additions & 2 deletions bonecp/src/main/java/com/jolbox/bonecp/ConnectionPartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,15 @@ protected void updateCreatedConnections(int increment) {

try{
this.statsLock.writeLock().lock();
this.createdConnections+=increment;
// assert this.createdConnections >= 0 : "Created connections < 0!";
if (this.createdConnections == 0 && increment < 0)
{
logger.error("The createdConnections can not be negative value");
}
else
{
this.createdConnections+=increment;
}


} finally {
this.statsLock.writeLock().unlock();
Expand Down
22 changes: 15 additions & 7 deletions bonecp/src/main/java/com/jolbox/bonecp/ConnectionTesterThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ public void run() {
connection.setOriginatingPartition(this.partition);

// check if connection has been idle for too long (or is marked as broken)
if (connection.isPossiblyBroken() ||
((this.idleMaxAgeInMs > 0) && ( System.currentTimeMillis()-connection.getConnectionLastUsedInMs() > this.idleMaxAgeInMs))){
// kill off this connection - it's broken or it has been idle for too long
if (connection.isPossiblyBroken())
{
// kill off this connection - it's broken, create a new one
closeConnection(connection);
createNew(connection);
continue;
}
else if (((this.idleMaxAgeInMs > 0) && ( System.currentTimeMillis()-connection.getConnectionLastUsedInMs() > this.idleMaxAgeInMs))){
// it has been idle for too long, kill it but don't recreate
closeConnection(connection);
continue;
}
Expand All @@ -102,6 +108,7 @@ public void run() {
// send a keep-alive, close off connection if we fail.
if (!this.pool.isConnectionHandleAlive(connection)){
closeConnection(connection);
createNew(connection);
continue;
}
// calculate the next time to wake up
Expand Down Expand Up @@ -148,7 +155,6 @@ public void run() {
}
}


/** Closes off this connection
* @param connection to close
*/
Expand All @@ -161,11 +167,13 @@ protected void closeConnection(ConnectionHandle connection) {
logger.error("Destroy connection exception", e);
} finally {
this.pool.postDestroyConnection(connection);
connection.getOriginatingPartition().getPoolWatchThreadSignalQueue().offer(new Object()); // item being pushed is not important.
}
}
}



private void createNew(ConnectionHandle connection)
{
connection.getOriginatingPartition().getPoolWatchThreadSignalQueue().offer(new Object());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,11 @@ public void testCloseConnection() {
this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool, 123, 123, false);
expect(mockConnection.isClosed()).andReturn(false);
ConnectionPartition mockPartition = EasyMock.createNiceMock(ConnectionPartition.class);
BlockingQueue<Object> mockQueue = EasyMock.createNiceMock(BlockingQueue.class);
expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition);

expect(mockPartition.getPoolWatchThreadSignalQueue()).andReturn(mockQueue);
expect(mockQueue.offer(anyObject())).andReturn(true).anyTimes();
mockPool.postDestroyConnection(mockConnection);
replay(mockConnection, mockPool, mockQueue, mockPartition);
replay(mockConnection, mockPool, mockPartition);
this.testClass.closeConnection(mockConnection);
verify(mockPool, mockConnection, mockQueue);
verify(mockPool, mockConnection);

}


Expand All @@ -406,15 +402,11 @@ public void testCloseConnectionWithException() throws SQLException {
mockConnection.internalClose();
expectLastCall().andThrow(new SQLException());
ConnectionPartition mockPartition = EasyMock.createNiceMock(ConnectionPartition.class);
BlockingQueue<Object> mockQueue = EasyMock.createNiceMock(BlockingQueue.class);
expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition);

expect(mockPartition.getPoolWatchThreadSignalQueue()).andReturn(mockQueue);
expect(mockQueue.offer(anyObject())).andReturn(true).anyTimes();
mockPool.postDestroyConnection(mockConnection);
replay(mockConnection, mockPool, mockQueue, mockPartition);
replay(mockConnection, mockPool, mockPartition);
this.testClass.closeConnection(mockConnection);
verify(mockPool, mockConnection, mockQueue);
verify(mockPool, mockConnection);
}

/**
Expand All @@ -429,11 +421,6 @@ public void testCloseConnectionWithExceptionInLogger() throws SQLException, NoSu
this.testClass = new ConnectionTesterThread(mockConnectionPartition, mockExecutor, mockPool, 123, 123, false);
expect(mockConnection.isClosed()).andReturn(false);
ConnectionPartition mockPartition = EasyMock.createNiceMock(ConnectionPartition.class);
BlockingQueue<Object> mockQueue = EasyMock.createNiceMock(BlockingQueue.class);
expect(mockConnection.getOriginatingPartition()).andReturn(mockPartition);

expect(mockPartition.getPoolWatchThreadSignalQueue()).andReturn(mockQueue);
expect(mockQueue.offer(anyObject())).andReturn(true).anyTimes();
mockPool.postDestroyConnection(mockConnection);
Field loggerField = this.testClass.getClass().getDeclaredField("logger");
TestUtils.setFinalStatic(loggerField, null);
Expand All @@ -442,13 +429,13 @@ public void testCloseConnectionWithExceptionInLogger() throws SQLException, NoSu
mockConnection.internalClose();
expectLastCall().andThrow(new SQLException());

replay(mockConnection, mockPool, mockQueue, mockPartition);
replay(mockConnection, mockPool, mockPartition);
try {
this.testClass.closeConnection(mockConnection);
}catch(NullPointerException e) {
//normal
}
verify(mockPool, mockConnection, mockQueue);
verify(mockPool, mockConnection);

}
@Test
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>com.jolbox</groupId>
<artifactId>bonecp-parent</artifactId>
<version>0.8.1-SNAPSHOT</version>
<version>0.8.0-patchidleconnection-SNAPSHOT</version>
<packaging>pom</packaging>

<name>BoneCP</name>
Expand Down