package com.roocon.thread.td2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyDataSource {
private LinkedList<Connection> pool = new LinkedList<>();
private static final int INIT_CONNECTIONS = 10;
private static final String DRIVER_CLASS = "";
private static final String USER = "";
private static final String PASSWORD = "";
private static final String URL = "";
private Lock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
static {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public MyDataSource() {
for (int i = 0; i < INIT_CONNECTIONS; i++) {
try {
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
pool.addLast(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// druid
public Connection getConnect() {
Connection result = null;
lock.lock();
try {
while (pool.size() <= 0) {
try {
c1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!pool.isEmpty()) {
result = pool.removeFirst();
}
return result;
} finally {
lock.unlock();
}
}
public void release(Connection conn) {
if (conn != null) {
lock.lock();
try {
pool.addLast(conn);
c1.signal();
} finally {
lock.unlock();
}
}
}
}
java数据库连接池
最新推荐文章于 2024-07-03 16:17:21 发布