BlockingQueue<Connection> connections = new LinkedBlockingQueue<Connection>(maxConnection);
AtomicInteger numberOfDrewedConnectionFromPool
在我的ConnectionPool我使用LinkedBlockingQueue。我怀疑是否乘法“if语句”是线程安全的。 maxConnection是恒定的。 numberOfDrewedConnectionFromPool也改变方法releaseConection无锁()..连接池
public Connection getConnection() throws ConnectionPoolException {
Connection connection = null;
if ((connections.poll() == null) && (maxConnection > numberOfDrewedConnectionFromPool.get())) {
return newConnection();
} else {
return connections.poll();
}
}
private Connection newConnection() throws ConnectionPoolException {
lock.lock();
Connection connection = null;
try {
try {
connection = DriverManager.getConnection(url, user, password);
numberOfDrewedConnectionFromPool.incrementAndGet();
} catch (SQLException exception) {
throw new ConnectionPoolException();
}
} finally {
lock.unlock();
return connection;
}
}
像JB Nizet建议的那样,使用现有的连接池,例如http://sourceforge.net/projects/c3p0/。自己实现这一点是浪费时间,如果不是不可能的话,很难做到。 – helpermethod
自己的conectionPool是任务的要求) – Ivan
它是否功课?如果不是这样,请向老板说明现有的连接池是否存在,并通过尝试实现连接池来损失资金。 –