1. 数据库连接池
不使用数据库连接池,传统获取连接存在的问题
- 普通的JDBC数据库连接使用 DriverManager 来获取,每次向数据库建立连接的时候都要将 Connection 加载到内存中,再验证用户名和密码(得花费0.05s~1s的时间)。需要数据库连接的时候,就向数据库要求一个,执行完成后再断开连接。这样的方式将会消耗大量的资源和时间。**数据库的连接资源并没有得到很好的重复利用。若同时有几百人甚至几千人在线,频繁的进行数据库连接操作将占用很多的系统资源,严重的甚至会造成服务器的崩溃。
- 对于每一次数据库连接,使用完后都得断开。否则,如果程序出现异常而未能关闭,将会导致数据库系统中的内存泄漏,最终将导致重启数据库。(回忆:何为Java的内存泄漏?)
- 这种开发不能控制被创建的连接对象数,系统资源会被毫无顾及的分配出去,如连接过多,也可能导致内存泄漏,服务器崩溃。
2. C3P0
```java package cn.java.money.jdbc.pool;<dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency>
import com.mchange.v2.c3p0.ComboPooledDataSource; import org.junit.jupiter.api.Test;
import java.sql.Connection; import java.sql.SQLException;
/**
- https://www.mchange.com/projects/c3p0/ *
https://github.com/swaldman/c3p0 */ public class C3P0Test { //方式一: @Test public void testGetConnection() throws Exception {
//获取c3p0数据库连接池, 本质上是 javax.sql.DataSourceComboPooledDataSource cpds = new ComboPooledDataSource();cpds.setDriverClass("com.mysql.cj.jdbc.Driver");cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=GMT");cpds.setUser("root");cpds.setPassword("123456");//通过设置相关的参数,对数据库连接池进行管理://设置初始时数据库连接池中的连接数cpds.setInitialPoolSize(10);Connection conn = cpds.getConnection();System.out.println(conn);//销毁c3p0数据库连接池(一般不需要)//DataSources.destroy( cpds );
}
//方式二:使用配置文件 @Test public void testGetConnection1() throws SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");Connection conn = cpds.getConnection();System.out.println(conn);
} }
[c3p0-config.xml](https://www.yuque.com/attachments/yuque/0/2022/xml/12535721/1644383600619-03e4b49c-b266-47b3-951b-1602451bfc81.xml?_lake_card=%7B%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2022%2Fxml%2F12535721%2F1644383600619-03e4b49c-b266-47b3-951b-1602451bfc81.xml%22%2C%22name%22%3A%22c3p0-config.xml%22%2C%22size%22%3A1249%2C%22type%22%3A%22text%2Fxml%22%2C%22ext%22%3A%22xml%22%2C%22status%22%3A%22done%22%2C%22taskId%22%3A%22u9aaae275-f66d-4394-b30a-e7a141ef36a%22%2C%22taskType%22%3A%22transfer%22%2C%22id%22%3A%22uf1324bfc%22%2C%22card%22%3A%22file%22%7D) 默认就是这个名字```xml<?xml version="1.0" encoding="UTF-8"?><c3p0-config><named-config name="hellc3p0"><!-- 提供获取连接的4个基本信息 --><property name="driverClass">com.mysql.cj.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=GMT</property><property name="user">root</property><property name="password">123456</property><!-- 进行数据库连接池管理的基本信息 --><!-- 当数据库连接池中的连接数不够时,c3p0一次性向数据库服务器申请的连接数 --><property name="acquireIncrement">5</property><!-- c3p0数据库连接池中初始化时的连接数 --><property name="initialPoolSize">10</property><!-- c3p0数据库连接池维护的最少连接数 --><property name="minPoolSize">10</property><!-- c3p0数据库连接池维护的最多的连接数 --><property name="maxPoolSize">100</property><!-- c3p0数据库连接池最多维护的Statement的个数 --><property name="maxStatements">50</property><!-- 每个连接中可以最多使用的Statement的个数 --><property name="maxStatementsPerConnection">2</property></named-config></c3p0-config>
3. DBCP
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId><version>2.8.0</version></dependency>
```java package cn.java.money.jdbc.pool;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSourceFactory; import org.junit.jupiter.api.Test;
import javax.sql.DataSource; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties;
/ http://commons.apache.org/proper/commons-dbcp/ / public class DBCPTest { / 测试DBCP的数据库连接池技术 */ //方式一:不推荐 @Test public void testGetConnection() throws SQLException { //创建了DBCP的数据库连接池 BasicDataSource source = new BasicDataSource();
//设置基本信息source.setDriverClassName("com.mysql.cj.jdbc.Driver");source.setUrl("jdbc:mysql:///test?serverTimezone=GMT");source.setUsername("root");source.setPassword("123456");//还可以设置其他涉及数据库连接池管理的相关属性:source.setInitialSize(10);source.setMaxTotal(10);Connection conn = source.getConnection();System.out.println(conn);}//方式二:推荐:使用配置文件@Testpublic void testGetConnection1() throws Exception {Properties pros = new Properties();//方式1://InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");//方式2:FileInputStream is =new FileInputStream(new File("src/main/resources/dbcp.properties"));pros.load(is);DataSource source = BasicDataSourceFactory.createDataSource(pros);Connection conn = source.getConnection();System.out.println(conn);}
}
dbcp.properties
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql:///test?serverTimezone=GMT username=root password=123456
initialSize=10
<a name="WE8lp"></a>#### 4. Druid```xml<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.4</version></dependency>
@Testpublic void getConnection() throws Exception{Properties pros = new Properties();InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");pros.load(is);DataSource source = DruidDataSourceFactory.createDataSource(pros);Connection conn = source.getConnection();System.out.println(conn);}
druid.properties
url=jdbc:mysql:///test?serverTimezone=GMTusername=rootpassword=123456driverClassName=com.mysql.cj.jdbc.DriverinitialSize=10maxActive=10
