1.为什么需要数据库连接池:
数据库连接是一种关键的、有限的、昂贵的资源。对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标。数据库连接池正是针对这个问题提出来的。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。
常见的数据库连接池有:dbcp、cp30、druid。其中Druid是阿里巴巴开源平台上一个数据库连接池实现,是“Java语言中最好的数据库连接池”。Druid能够提供强大的监控和扩展功能。源码地址:https://github.com/alibaba/druid。
2.实现自己的数据库连接池
参考okhttp3的ConnectionPool以及RealConnection的实现,学习okhttp3网络连接池的思想,实现数据库连接池。
ConnectionPool维护一个RealConnection的列表。
在增加新的连接时会清除空闲时间超过最大空闲时间的连接,保证资源的有效回收和内存的高效利用。
在提供连接是会返回负载最低的连接,保证所有连接的负载均衡。
public final class ConnectionPool {
private final Deque<RealConnection> connections = new ArrayDeque<>();
private static final Executor executor = new ThreadPoolExecutor(
0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
private final int maxIdleConnections;
private final int corePoolSize;
private final long keepAliveDurationNs;
private boolean cleanupRunning;
public ConnectionPool() {
this(5, 5, 10, TimeUnit.MINUTES);
}
public ConnectionPool(int corePoolSize, int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
this.corePoolSize = corePoolSize;
this.maxIdleConnections = maxIdleConnections;
this.keepAliveDurationNs = timeUnit.toNanos(keepAliveDuration);
for (int i = 0; i < corePoolSize; i++) {
connections.add(new RealConnection());
}
if (keepAliveDuration <= 0) {
throw new IllegalArgumentException("keepAliveDuration <= 0: " + keepAliveDuration);
}
}
public synchronized int idleConnectionCount() {
int total = 0;
for (RealConnection connection : connections) {
if (connection.getCount() <= 0) total++;
}
return total;
}
public synchronized int connectionCount() {
return connections.size();
}
@Nullable
RealConnection get() {
if (connections.size() < corePoolSize || connections.isEmpty()) {
RealConnection connection = new RealConnection();
put(connection);
return connection;
}
int minCount = Integer.MAX_VALUE;
RealConnection minCountConnection = connections.getFirst();
for (RealConnection connection : connections) {
if (connection.getCount() < minCount) {
minCount = connection.getCount();
minCountConnection = connection;
}
}
return minCountConnection;
}
void put(RealConnection connection) {
if (!cleanupRunning) {
cleanupRunning = true;
executor.execute(cleanupRunnable);
}
connections.add(connection);
}
boolean connectionBecameIdle(RealConnection connection) {
if (connection.getCount() <= 0 || maxIdleConnections == 0) {
connection.close();
connections.remove(connection);
return true;
} else {
notifyAll();
return false;
}
}
public void evictAll() {
synchronized (this) {
for (Iterator<RealConnection> i = connections.iterator(); i.hasNext(); ) {
RealConnection connection = i.next();
if (connection.getCount() <= 0) {
connection.close();
i.remove();
}
}
}
}
long cleanup(long now) {
int inUseConnectionCount = 0;
int idleConnectionCount = 0;
RealConnection longestIdleConnection = null;
long longestIdleDurationNs = Long.MIN_VALUE;
// Find either a connection to evict, or the time that the next eviction is due.
synchronized (this) {
for (Iterator<RealConnection> i = connections.iterator(); i.hasNext(); ) {
RealConnection connection = i.next();
// If the connection is in use, keep searching.
if (connection.getCount() > 0) {
inUseConnectionCount++;
continue;
}
idleConnectionCount++;
// If the connection is ready to be evicted, we're done.
long idleDurationNs = now - connection.getIdleAtNanos();
if (idleDurationNs > longestIdleDurationNs) {
longestIdleDurationNs = idleDurationNs;
longestIdleConnection = connection;
}
}
if (longestIdleDurationNs >= this.keepAliveDurationNs
|| idleConnectionCount > this.maxIdleConnections) {
// We've found a connection to evict. Remove it from the list, then close it below (outside
// of the synchronized block).
connections.remove(longestIdleConnection);
} else if (idleConnectionCount > 0) {
// A connection will be ready to evict soon.
return keepAliveDurationNs - longestIdleDurationNs;
} else if (inUseConnectionCount > 0) {
// All connections are in use. It'll be at least the keep alive duration 'til we run again.
return keepAliveDurationNs;
} else {
// No connections, idle or in use.
cleanupRunning = false;
return -1;
}
}
longestIdleConnection.close();
return 0;
}
private final Runnable cleanupRunnable = new Runnable() {
@Override
public void run() {
while (true) {
long waitNanos = cleanup(System.nanoTime());
if (waitNanos == -1) return;
if (waitNanos > 0) {
long waitMillis = waitNanos / 1000000L;
waitNanos -= (waitMillis * 1000000L);
synchronized (ConnectionPool.this) {
try {
ConnectionPool.this.wait(waitMillis, (int) waitNanos);
} catch (InterruptedException ignored) {
}
}
}
}
}
};
}