因此,我使用填充了SQLConnection对象列表的阻塞队列创建了自己的MySQL连接池:
public class SQLConnection {
private Connection connection;
private int uses;
private long alive;
protected SQLConnection() {
try {
this.connection = DriverManager.getConnection(SQLPool.URL, SQLPool.USER, SQLPool.PASS);
} catch (SQLException e) {
e.printStackTrace();
}
this.uses = 0;
this.alive = System.currentTimeMillis();
}
protected void addUse() {
uses++;
}
protected boolean isRenew() {
try {
if (connection.isClosed() || uses > 100 || getAlive() > 900000) {
return true;
}
} catch (Exception e) {
return true;
}
return false;
}
protected boolean isAvailable() {
try {
return !connection.isClosed();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
该池(未显示代码)具有retrieveResource()和returnResource(SQLConnection conn)方法以及BlockingQueue。它似乎运行良好,并且在其存活时间超过15分钟或有100次使用后,将其从池中转储并添加了新实例。 由于我的项目必须非常扎实,我想知道这是否会引起任何明显的问题?