Recycle JDBC connections idle for more than 60 seconds
This commit is contained in:
parent
0530bf07cc
commit
862a8f68dd
4 changed files with 64 additions and 11 deletions
|
|
@ -49,6 +49,8 @@ public class MicrosoftSQLMapStorage extends MapStorage {
|
||||||
protected int port;
|
protected int port;
|
||||||
private static final int POOLSIZE = 5;
|
private static final int POOLSIZE = 5;
|
||||||
private Connection[] cpool = new Connection[POOLSIZE];
|
private Connection[] cpool = new Connection[POOLSIZE];
|
||||||
|
private long[] cpoolLastUseTS = new long[POOLSIZE]; // Time when last returned to pool
|
||||||
|
private static final long IDLE_TIMEOUT = 60000; // Use 60 second timeout
|
||||||
private int cpoolCount = 0;
|
private int cpoolCount = 0;
|
||||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
|
@ -527,12 +529,22 @@ public class MicrosoftSQLMapStorage extends MapStorage {
|
||||||
private Connection getConnection() throws SQLException {
|
private Connection getConnection() throws SQLException {
|
||||||
Connection c = null;
|
Connection c = null;
|
||||||
synchronized (cpool) {
|
synchronized (cpool) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
while (c == null) {
|
while (c == null) {
|
||||||
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
||||||
if (cpool[i] != null) { // Found one
|
if (cpool[i] != null) { // Found one
|
||||||
c = cpool[i];
|
// If in pool too long, close it and move on
|
||||||
cpool[i] = null;
|
if ((now - cpoolLastUseTS[i]) > IDLE_TIMEOUT) {
|
||||||
break;
|
try { cpool[i].close(); } catch (SQLException x) {}
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolCount--;
|
||||||
|
}
|
||||||
|
else { // Else, use the connection
|
||||||
|
c = cpool[i];
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolLastUseTS[i] = now;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
|
|
@ -565,6 +577,7 @@ public class MicrosoftSQLMapStorage extends MapStorage {
|
||||||
for (int i = 0; i < POOLSIZE; i++) {
|
for (int i = 0; i < POOLSIZE; i++) {
|
||||||
if (cpool[i] == null) {
|
if (cpool[i] == null) {
|
||||||
cpool[i] = c;
|
cpool[i] = c;
|
||||||
|
cpoolLastUseTS[i] = System.currentTimeMillis(); // Record last use time
|
||||||
c = null; // Mark it recovered (no close needed
|
c = null; // Mark it recovered (no close needed
|
||||||
cpool.notifyAll();
|
cpool.notifyAll();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,8 @@ public class MySQLMapStorage extends MapStorage {
|
||||||
protected int port;
|
protected int port;
|
||||||
private static final int POOLSIZE = 5;
|
private static final int POOLSIZE = 5;
|
||||||
private Connection[] cpool = new Connection[POOLSIZE];
|
private Connection[] cpool = new Connection[POOLSIZE];
|
||||||
|
private long[] cpoolLastUseTS = new long[POOLSIZE]; // Time when last returned to pool
|
||||||
|
private static final long IDLE_TIMEOUT = 60000; // Use 60 second timeout
|
||||||
private int cpoolCount = 0;
|
private int cpoolCount = 0;
|
||||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
|
@ -647,12 +649,22 @@ public class MySQLMapStorage extends MapStorage {
|
||||||
Connection c = null;
|
Connection c = null;
|
||||||
if (isShutdown) { throw new StorageShutdownException(); }
|
if (isShutdown) { throw new StorageShutdownException(); }
|
||||||
synchronized (cpool) {
|
synchronized (cpool) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
while (c == null) {
|
while (c == null) {
|
||||||
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
||||||
if (cpool[i] != null) { // Found one
|
if (cpool[i] != null) { // Found one
|
||||||
c = cpool[i];
|
// If in pool too long, close it and move on
|
||||||
cpool[i] = null;
|
if ((now - cpoolLastUseTS[i]) > IDLE_TIMEOUT) {
|
||||||
break;
|
try { cpool[i].close(); } catch (SQLException x) {}
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolCount--;
|
||||||
|
}
|
||||||
|
else { // Else, use the connection
|
||||||
|
c = cpool[i];
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolLastUseTS[i] = now;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
|
|
@ -685,6 +697,7 @@ public class MySQLMapStorage extends MapStorage {
|
||||||
for (int i = 0; i < POOLSIZE; i++) {
|
for (int i = 0; i < POOLSIZE; i++) {
|
||||||
if (cpool[i] == null) {
|
if (cpool[i] == null) {
|
||||||
cpool[i] = c;
|
cpool[i] = c;
|
||||||
|
cpoolLastUseTS[i] = System.currentTimeMillis(); // Record last use time
|
||||||
c = null; // Mark it recovered (no close needed
|
c = null; // Mark it recovered (no close needed
|
||||||
cpool.notifyAll();
|
cpool.notifyAll();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,8 @@ public class PostgreSQLMapStorage extends MapStorage {
|
||||||
private int port;
|
private int port;
|
||||||
private static final int POOLSIZE = 5;
|
private static final int POOLSIZE = 5;
|
||||||
private Connection[] cpool = new Connection[POOLSIZE];
|
private Connection[] cpool = new Connection[POOLSIZE];
|
||||||
|
private long[] cpoolLastUseTS = new long[POOLSIZE]; // Time when last returned to pool
|
||||||
|
private static final long IDLE_TIMEOUT = 60000; // Use 60 second timeout
|
||||||
private int cpoolCount = 0;
|
private int cpoolCount = 0;
|
||||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
|
@ -571,12 +573,22 @@ public class PostgreSQLMapStorage extends MapStorage {
|
||||||
Connection c = null;
|
Connection c = null;
|
||||||
if (isShutdown) throw new StorageShutdownException();
|
if (isShutdown) throw new StorageShutdownException();
|
||||||
synchronized (cpool) {
|
synchronized (cpool) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
while (c == null) {
|
while (c == null) {
|
||||||
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
||||||
if (cpool[i] != null) { // Found one
|
if (cpool[i] != null) { // Found one
|
||||||
c = cpool[i];
|
// If in pool too long, close it and move on
|
||||||
cpool[i] = null;
|
if ((now - cpoolLastUseTS[i]) > IDLE_TIMEOUT) {
|
||||||
break;
|
try { cpool[i].close(); } catch (SQLException x) {}
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolCount--;
|
||||||
|
}
|
||||||
|
else { // Else, use the connection
|
||||||
|
c = cpool[i];
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolLastUseTS[i] = now;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
|
|
@ -608,6 +620,7 @@ public class PostgreSQLMapStorage extends MapStorage {
|
||||||
for (int i = 0; i < POOLSIZE; i++) {
|
for (int i = 0; i < POOLSIZE; i++) {
|
||||||
if (cpool[i] == null) {
|
if (cpool[i] == null) {
|
||||||
cpool[i] = c;
|
cpool[i] = c;
|
||||||
|
cpoolLastUseTS[i] = System.currentTimeMillis(); // Record last use time
|
||||||
c = null; // Mark it recovered (no close needed
|
c = null; // Mark it recovered (no close needed
|
||||||
cpool.notifyAll();
|
cpool.notifyAll();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ public class SQLiteMapStorage extends MapStorage {
|
||||||
private String databaseFile;
|
private String databaseFile;
|
||||||
private static final int POOLSIZE = 1; // SQLite is really not thread safe... 1 at a time works best
|
private static final int POOLSIZE = 1; // SQLite is really not thread safe... 1 at a time works best
|
||||||
private Connection[] cpool = new Connection[POOLSIZE];
|
private Connection[] cpool = new Connection[POOLSIZE];
|
||||||
|
private long[] cpoolLastUseTS = new long[POOLSIZE]; // Time when last returned to pool
|
||||||
|
private static final long IDLE_TIMEOUT = 60000; // Use 60 second timeout
|
||||||
private int cpoolCount = 0;
|
private int cpoolCount = 0;
|
||||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
|
@ -492,11 +494,22 @@ public class SQLiteMapStorage extends MapStorage {
|
||||||
throw new StorageShutdownException();
|
throw new StorageShutdownException();
|
||||||
}
|
}
|
||||||
synchronized (cpool) {
|
synchronized (cpool) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
while (c == null) {
|
while (c == null) {
|
||||||
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
for (int i = 0; i < cpool.length; i++) { // See if available connection
|
||||||
if (cpool[i] != null) { // Found one
|
if (cpool[i] != null) { // Found one
|
||||||
c = cpool[i];
|
// If in pool too long, close it and move on
|
||||||
cpool[i] = null;
|
if ((now - cpoolLastUseTS[i]) > IDLE_TIMEOUT) {
|
||||||
|
try { cpool[i].close(); } catch (SQLException x) {}
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolCount--;
|
||||||
|
}
|
||||||
|
else { // Else, use the connection
|
||||||
|
c = cpool[i];
|
||||||
|
cpool[i] = null;
|
||||||
|
cpoolLastUseTS[i] = now;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
|
|
@ -532,6 +545,7 @@ public class SQLiteMapStorage extends MapStorage {
|
||||||
for (int i = 0; i < POOLSIZE; i++) {
|
for (int i = 0; i < POOLSIZE; i++) {
|
||||||
if (cpool[i] == null) {
|
if (cpool[i] == null) {
|
||||||
cpool[i] = c;
|
cpool[i] = c;
|
||||||
|
cpoolLastUseTS[i] = System.currentTimeMillis(); // Record last use time
|
||||||
c = null; // Mark it recovered (no close needed
|
c = null; // Mark it recovered (no close needed
|
||||||
cpool.notifyAll();
|
cpool.notifyAll();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue