JDBC 数据库连接池的简单实现
连接池代码:
public class MyDataSource2{
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "";
private static int initCount = 1;
private static int maxCount = 1;
int currentCount = 0;
LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
public MyDataSource2() {
try {
for (int i = 0; i < initCount; i++) {
this.connectionsPool.addLast(this.createConnection());
this.currentCount++;
}
} catch (SQLException e) {
throw new ExceptionInInitializerError(e);
}
}
public Connection getConnection() throws SQLException {
synchronized (connectionsPool) {
if (this.connectionsPool.size() > 0)
return this.connectionsPool.removeFirst();
if (this.currentCount < maxCount) {
this.currentCount++;
return this.createConnection();
}
throw new SQLException("已没有链接");
}
}
public void free(Connection conn) {
this.connectionsPool.addLast(conn);
}
private Connection createConnection() throws SQLException {
Connection realConn = DriverManager.getConnection(url, user, password);
MyConnection myConnection = new MyConnection(realConn, this);
return myConnection;
}
//自建Connection类,重写了close方法,并且限定了同一个Connection的最大使用次数,其它的都使用realConnection的方法
public class MyConnection implements Connection {
private Connection realConnection;
private MyDataSource2 dataSource;
private int maxUseCount = 5;
private int currentUserCount = 0;
MyConnection(Connection connection, MyDataSource2 dataSource) {
this.realConnection = connection;
this.dataSource = dataSource;
}
public void close() throws SQLException {
this.currentUserCount++;
if (this.currentUserCount < this.maxUseCount)
this.dataSource.connectionsPool.addLast(this);
else {
this.realConnection.close();
this.dataSource.currentCount--;//这段是限定同一个链接的使用次数,如果这个MyConnection对象被重复使用过好多次了,就释放了它。
//但是这么做有什么意义?同一个链接使用了很多次会造成什么严重后果吗?这块我真是百思不得其解
}
}
……
}
public final class JdbcUtils {
private static MyDataSource2 myDataSource = null;
private JdbcUtils() {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
myDataSource = new MyDataSource2();//放在这个地方,要加载后才能初始化
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource() {
return myDataSource;
}
public static Connection getConnection() throws SQLException {
return myDataSource.getConnection();
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
相关文章
- MySQL_(Java)使用JDBC向数据库中修改(update)数据
- MySQL_(Java)使用JDBC向数据库中删除(delete)数据
- MySQL_(Java)使用JDBC向数据库中插入(insert)数据
- 一文读懂数据库优化之分库分表
- 使用JDBC在MySQL数据库中快速批量插入数据
- JDBC:数据库操作:BLOB数据处理
- Java -- JDBC 学习--数据库连接池
- [Servlet&JSP] 使用JDBC连接数据库
- JDBC连接oracle RAC数据库配置
- spring boot:配置druid数据库连接池(开启sql防火墙/使用log4j2做异步日志/spring boot 2.3.2)
- Navicat导入数据库数据结构sql报错datetime(0)
- Java JDBC篇4——数据库连接池
- 如何让Activiti-Explorer使用sql server数据库
- SAP CRM中间件Consolidated数据库的实现原理
- Atitit. 数据库-----catalog与schema的设计区别以及在实际中使用 获取数据库所有库表 java jdbc php c#.Net
- Atitit.跨语言数据库db api兼容性 jdbc odbc ado oledb 增强方案
- Atitit.跨语言数据库db api兼容性 jdbc odbc ado oledb 增强方案
- JDBC连接数据库
- sql sever——创建数据库
- Shell脚本一键安装-----MYSQL数据库