public class DBPool implements DataSource{
private LinkedList<Connection> pool = new LinkedList<>();
public DBPool() {
try {
Properties properties = new Properties();
InputStream is = ConnectionUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(is);
String driver = properties.getProperty("DRIVER");
String url = properties.getProperty("URL");
String user = properties.getProperty("USER");
String password = properties.getProperty("PASSWORD");
Class.forName(driver);
for(int i =0;i<3;i++) {
final Connection connection = DriverManager.getConnection(url, user, password);
Object proxyObject = Proxy.newProxyInstance(DBPool.class.getClassLoader(), new Class[] {Connection.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
if(method.getName().equals("close")) {
synchronized (pool) {
pool.add((Connection)proxy);
pool.notify();
}
return null;
}else {
return method.invoke(connection,args);
}
}
});
pool.add((Connection)proxyObject);
}
}catch (Exception e) {
// TODO: handle exception
throw new RuntimeException();
}
}
@Override
public Connection getConnection() throws SQLException {
synchronized(pool) {
if(pool.size()==0) {
try {
pool.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getConnection();
}
}
return pool.removeFirst();
}
}
自己实现一个连接池工具类
public class ConnectionUtils {
//声明连接池
private static LinkedList<Connection> pool = new LinkedList<>();
//在静态代码块创建多个连接
static {
try {
Properties properties = new Properties();
InputStream is = ConnectionUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(is);
String driver = properties.getProperty("DRIVER");
String url = properties.getProperty("URL");
String user = properties.getProperty("USER");
String password = properties.getProperty("PASSWORD");
Class.forName(driver);
for(int i=0;i<3;i++) {
//被代理的对象声明
final Connection conn = DriverManager.getConnection(url, user, password);
//创建代理类
Object proxyObj = Proxy.newProxyInstance(ConnectionUtils.class.getClassLoader(), new Class[] {Connection.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
//拦截close方法
if(method.getName().equals("close")) {
//不关闭连接 而是放到pool里面
synchronized (pool) {
pool.add((Connection)proxy);
pool.notify();//唤醒
}
return null;
}else {
return method.invoke(conn, args);
}
}
});
//将代理的连接对象放到连接池
pool.add((Connection)proxyObj);
}
}catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);
}
}
/**
* 提供一个静态工厂 返回一个连接
* @return
*/
public static Connection getConn() {
synchronized (pool) {
if(pool.size()==0) {
try {
pool.wait();//等待直到还了一个后
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getConn();
}
Connection connection = pool.removeFirst();
System.out.println("size="+pool.size());
return connection;
}
}
}