Skip to content

Commit

Permalink
improve scheduling of check tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Jan 14, 2014
1 parent 71ef42c commit fab0980
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 32 deletions.
10 changes: 4 additions & 6 deletions bonecp/src/main/java/com/jolbox/bonecp/BoneCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.jolbox.bonecp;

import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.ref.Reference;
Expand Down Expand Up @@ -321,7 +320,6 @@ protected Connection obtainInternalConnection(ConnectionHandle connectionHandle)
* @return Connection handle
* @throws SQLException on error
*/
@SuppressWarnings("resource")
protected Connection obtainRawInternalConnection()
throws SQLException {
Connection result = null;
Expand Down Expand Up @@ -473,7 +471,7 @@ public BoneCP(BoneCPConfig config) throws SQLException {

if (this.config.getIdleConnectionTestPeriod(TimeUnit.SECONDS) > 0 || this.config.getIdleMaxAge(TimeUnit.SECONDS) > 0){

final Runnable connectionTester = new ConnectionTesterThread(connectionPartition, this.keepAliveScheduler, this, this.config.getIdleMaxAge(TimeUnit.MILLISECONDS), this.config.getIdleConnectionTestPeriod(TimeUnit.MILLISECONDS), queueLIFO);
final Runnable connectionTester = new ConnectionTesterThread(connectionPartition, this, this.config.getIdleMaxAge(TimeUnit.MILLISECONDS), this.config.getIdleConnectionTestPeriod(TimeUnit.MILLISECONDS), queueLIFO);
long delayInSeconds = this.config.getIdleConnectionTestPeriod(TimeUnit.SECONDS);
if (delayInSeconds == 0L){
delayInSeconds = this.config.getIdleMaxAge(TimeUnit.SECONDS);
Expand All @@ -483,13 +481,13 @@ public BoneCP(BoneCPConfig config) throws SQLException {
&& this.config.getIdleMaxAge(TimeUnit.SECONDS) != 0){
delayInSeconds = this.config.getIdleMaxAge(TimeUnit.SECONDS);
}
this.keepAliveScheduler.schedule(connectionTester, delayInSeconds, TimeUnit.SECONDS);
this.keepAliveScheduler.scheduleAtFixedRate(connectionTester,delayInSeconds, delayInSeconds, TimeUnit.SECONDS);
}


if (this.config.getMaxConnectionAgeInSeconds() > 0){
final Runnable connectionMaxAgeTester = new ConnectionMaxAgeThread(connectionPartition, this.maxAliveScheduler, this, this.config.getMaxConnectionAge(TimeUnit.MILLISECONDS), queueLIFO);
this.maxAliveScheduler.schedule(connectionMaxAgeTester, this.config.getMaxConnectionAgeInSeconds(), TimeUnit.SECONDS);
final Runnable connectionMaxAgeTester = new ConnectionMaxAgeThread(connectionPartition, this, this.config.getMaxConnectionAge(TimeUnit.MILLISECONDS), queueLIFO);
this.maxAliveScheduler.scheduleAtFixedRate(connectionMaxAgeTester, this.config.getMaxConnectionAgeInSeconds(), this.config.getMaxConnectionAgeInSeconds(), TimeUnit.SECONDS);
}
// watch this partition for low no of threads
this.connectionsScheduler.execute(new PoolWatchThread(connectionPartition, this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package com.jolbox.bonecp;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,8 +32,6 @@ public class ConnectionMaxAgeThread implements Runnable {
private long maxAgeInMs;
/** Partition being handled. */
private ConnectionPartition partition;
/** Scheduler handle. **/
private ScheduledExecutorService scheduler;
/** Handle to connection pool. */
private BoneCP pool;
/** If true, we're operating in a LIFO fashion. */
Expand All @@ -50,10 +46,9 @@ public class ConnectionMaxAgeThread implements Runnable {
* @param maxAgeInMs Threads older than this are killed off
* @param lifoMode if true, we're running under a lifo fashion.
*/
protected ConnectionMaxAgeThread(ConnectionPartition connectionPartition, ScheduledExecutorService scheduler,
protected ConnectionMaxAgeThread(ConnectionPartition connectionPartition,
BoneCP pool, long maxAgeInMs, boolean lifoMode){
this.partition = connectionPartition;
this.scheduler = scheduler;
this.maxAgeInMs = maxAgeInMs;
this.pool = pool;
this.lifoMode = lifoMode;
Expand Down Expand Up @@ -102,19 +97,11 @@ public void run() {
Thread.sleep(20L); // test slowly, this is not an operation that we're in a hurry to deal with (avoid CPU spikes)...
}
} catch (Throwable e) {
if (this.scheduler.isShutdown()){
logger.debug("Shutting down connection max age thread.");
} else {
logger.error("Connection max age thread exception.", e);
}
}

} // throw it back on the queue

if (!this.scheduler.isShutdown()){
this.scheduler.schedule(this, nextCheckInMs, TimeUnit.MILLISECONDS);
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.jolbox.bonecp;

import java.sql.SQLException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,8 +35,6 @@ public class ConnectionTesterThread implements Runnable {
private long idleMaxAgeInMs;
/** Partition being handled. */
private ConnectionPartition partition;
/** Scheduler handle. **/
private ScheduledExecutorService scheduler;
/** Handle to connection pool. */
private BoneCP pool;
/** If true, we're operating in a LIFO fashion. */
Expand All @@ -48,16 +44,14 @@ public class ConnectionTesterThread implements Runnable {

/** Constructor
* @param connectionPartition partition to work on
* @param scheduler Scheduler handler.
* @param pool pool handle
* @param idleMaxAgeInMs Threads older than this are killed off
* @param idleConnectionTestPeriodInMs Threads that are idle for more than this time are sent a keep-alive.
* @param lifoMode if true, we're running under a lifo fashion.
*/
protected ConnectionTesterThread(ConnectionPartition connectionPartition, ScheduledExecutorService scheduler,
protected ConnectionTesterThread(ConnectionPartition connectionPartition,
BoneCP pool, long idleMaxAgeInMs, long idleConnectionTestPeriodInMs, boolean lifoMode){
this.partition = connectionPartition;
this.scheduler = scheduler;
this.idleMaxAgeInMs = idleMaxAgeInMs;
this.idleConnectionTestPeriodInMs = idleConnectionTestPeriodInMs;
this.pool = pool;
Expand Down Expand Up @@ -138,13 +132,8 @@ public void run() {
// offset by a bit to avoid firing a lot for slightly offset connections
// logger.debug("Next check in "+nextCheckInMs);

this.scheduler.schedule(this, nextCheckInMs, TimeUnit.MILLISECONDS);
} catch (Throwable e) {
if (this.scheduler.isShutdown()){
logger.debug("Shutting down connection tester thread.");
} else {
logger.error("Connection tester thread interrupted", e);
}
}
}

Expand Down

0 comments on commit fab0980

Please sign in to comment.