The database connection pool is responsible for allocating, managing, and releasing database connections. It allows applications to reuse an existing database connection instead of re-establishing one; release database connections whose idle time exceeds the maximum idle time to avoid failure due to failure to release database connections. Caused by missing database connections. This technique can significantly improve the performance of database operations.
必须实现 javax.sql.DataSource 接口
获取连接:getConnection();
归还连接:conn.close();
方法增前的方式:
1. 继承
2. 静态代理
3. 动态代理
静态代理步骤:
1. 要求装饰者和被装饰者实现同一个接口或者继承同一个类
2. 要求装饰者要有被装饰者的引用
3. 对需要加强的方法进行加强
4. 对不需要加强的方法调用原来的方法
常见的连接池:
1. DBCP
使用步骤
1. 导入 jar 包(两个 jar 包)
2. 编码
a. 硬编码
new BasicDataSource()
b. 配置文件
Properties prop = new Properties()
prop.load(is);
new BasicDataSourceFactory().createDataSource(Properties prop);
2. C3P0
使用步骤
1. 导入 jar 包(1个)
2. 编码
a. 配置文件
配置文件的名称:c3p0.properties 或者c3p0-config.xml
配置文件的位置:src 目录下
编码中通过构造器创建
new ComboPooledDataSource();