介绍
数据库的连接创建工作比较消耗性能,一开始创建多个连接,在内存中开辟一块空间,往空间里存放这些连接对象,使用时,从池中去连接对象,使用完毕,归还连接。
JdbcUtil
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
/**
* jdbc连接工具类
*
* @author mahao
* @date 2019年5月2日 下午5:35:30
*/
public class JdbcUtil {
//数据库连接地址
public static String URL;
//用户名
public static String USERNAME;
//密码
public static String PASSWORD;
//MYSQL驱动类
public static String SERVER;
//加载属性资源
private static ResourceBundle rb = ResourceBundle.getBundle("jdbc");
//静态块加载驱动程序
static{
URL = rb.getString("jdbc.url");
USERNAME = rb.getString("jdbc.username");
PASSWORD = rb.getString("jdbc.password");
SERVER = rb.getString("jdbc.server");
try {
Class.forName(SERVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//定义一个获取数据库连接的方法
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库
public static void close(ResultSet rs,Statement st,Connection conn){
try {
if(rs != null)rs.close();
if(st != null)st.close();
if(conn != null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
MyDataSource
package pool;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.sql.DataSource;
import util.JdbcUtil;
/**
* 连接池: 使用动态代理模式,也可以使用装饰者模式
*
* @author mahao
* @date 2019年5月3日 上午10:42:07
*/
public class MyDataSource implements DataSource {
private static MyDataSource instance = null;
private List<Connection> pool = new LinkedList<>();
private MyDataSource(int num) {
for (int i = 0; i < num; i++) {
Connection conn = JdbcUtil.getConnection();
Connection proxy = (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(),
new Class[]{Connection.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("close")) {
pool.add(conn);
return null;
} else {
return method.invoke(conn, args);
}
}
});
pool.add(proxy);
}
}
public static MyDataSource createPool() {
if (instance == null) {
synchronized (MyDataSource.class) {
if (instance == null) {
instance = new MyDataSource(15);
}
}
}
return instance;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Connection getConnection() throws SQLException {
System.out.println("连接池中大小是----" + pool.size());
if (pool.size() == 0) {
addCapacity(5);
}
return pool.remove(0);
}
private void addCapacity(int i) {
Connection conn = JdbcUtil.getConnection();
Connection proxy = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
Connection.class.getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("close")) {
pool.add(conn);
}
return method.invoke(conn, args);
}
});
pool.add(proxy);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
}