JDBC数据库连接池
常用的数据库连接池
- DBCP:APache提供的数据库连接池。tomcat服务器端自带的dpcp数据库连接池,速度较C3P0快
- C3P0:是一个开源组织提供的一个数据库连接池,速度相对较慢,稳定性还可以
- Druid:是阿里提供的数据库连接池
JDBC的数据库连接池使用javax.sql.DataSource表示,DataSource只是一个接口
C3P0测试:
首先导入jar包
方式一:
public void testGetConnection() throws Exception {
//获取C3P0数据库连接池
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" );
cpds.setUser("root");
cpds.setPassword("1234");
//设置连接池初始化连接数
cpds.setInitialPoolSize(10);
//获取连接
Connection conn = cpds.getConnection();
System.out.println(conn);
}
方式二:使用配置文件将基本信息放至文件中,新建XML文件,文件配置如下
<c3p0-config>
<named-config name="helloC3P0">
<!-- 提供连接获取的四个基本信息 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">1234</property>
<!-- 进行数据库连接池管理的基本信息 -->
<!-- 当数据库连接池的连接数不够时,C3P0一次性向数据库服务器申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- C3P0数据库连接池中初始化时的连接数 -->
<property name="initialPoolSize">100</property>
<!-- C3P0数据库连接池中维护的最小连接数 -->
<property name="minPoolSize">10</property>
<!-- C3P0数据库连接池中维护的最多连接数 -->
<property name="maxPoolSize">1000</property>
<!-- C3P0数据库连接池中维护的最多的Statement个数 -->
<property name="maxStatements">50</property>
<!-- 每个连接可以使用的最多的Statement的个数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
使用配置文件获取数据库连接池
public void testGetConnection1() throws Exception {
ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
Connection conn = cpds.getConnection();
System.out.println(conn);
}
使用C3P0数据库连接池获取一个数据库连接
public static Connection getConnection1() throws Exception {
ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
Connection conn = cpds.getConnection();
return conn;
}
可以改进:
//数据库连接池只需要提供一个即可
private static ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
public static Connection getConnection1() throws Exception {
Connection conn = cpds.getConnection();
return conn;
}
DBCP
导入jar包
方式一:不推荐
public void testGetConnection() throws Exception {
//创建了DBCP的数据库连接池
BasicDataSource source = new BasicDataSource();
//设置基本的信息
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/test");
source.setUsername("root");
source.setPassword("1234");
//设置其他涉及数据库连接池管理的配置属性
source.setInitialSize(10);
source.setMaxActive(10);
Connection conn = source.getConnection();
System.out.println(conn);
}
DBCP常用的数据库连接池管理的配置属性
dbcp连接池常用基本配置属性
1.initialSize :连接池启动时创建的初始化连接数量(默认值为0)
2.maxActive :连接池中可同时连接的最大的连接数(默认值为8,调整为20,高峰单机器在20并发左右,自己根据应用场景定)
3.maxIdle:连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制(默认为8个,maxIdle不能设置太小,因为假如在高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中idle的个数 上升超过maxIdle,而造成频繁的连接销毁和创建,类似于jvm参数中的Xmx设置)
4.minIdle:连接池中最小的空闲的连接数,低于这个数量会被创建新的连接(默认为0,调整为5,该参数越接近maxIdle,性能越好,因为连接的创建和销毁,都是需要消耗资源的;但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接,类似于jvm参数中的Xmn设置)
5.maxWait :最大等待时间,当没有可用连接时,连接池等待连接释放的最大时间,超过该时间限制会抛出异常,如果设置-1表示无限等待(默认为无限,调整为60000ms,避免因线程池不够用,而导致请求被无限制挂起)
6.poolPreparedStatements:开启池的prepared(默认是false,未调整,经过测试,开启后的性能没有关闭的好。)
7.maxOpenPreparedStatements:开启池的prepared 后的同时最大连接数(默认无限制,同上,未配置)
8.minEvictableIdleTimeMillis :连接池中连接,在时间段内一直空闲, 被逐出连接池的时间
9.removeAbandonedTimeout :超过时间限制,回收没有用(废弃)的连接(默认为 300秒,调整为180)
10.removeAbandoned :超过removeAbandonedTimeout时间后,是否进 行没用连接(废弃)的回收(默认为false,调整为true)
方式二:使用配置文件,推荐
创建dbcp.properties文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=1234
//配置属性可以选填
initialSize=10
JDBC
public void testGetConnection1() throws Exception {
Properties pros = new Properties();
//方式一和方式二都可以
//方式一:
// InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("DBCP.properties");
//方式二:
FileInputStream is = new FileInputStream(new File("src/DBCP.properties"));
pros.load(is);
DataSource source = BasicDataSourceFactory.createDataSource(pros);
Connection conn = source.getConnection();
System.out.println(conn);
}
Druid
public void testGetConnection() throws Exception {
Properties pros = new Properties();
// InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
//方式二
FileInputStream is = new FileInputStream(new File("src/druid.properties"));
pros.load(is);
DataSource source = DruidDataSourceFactory.createDataSource(pros);
Connection conn = source.getConnection();
System.out.println(conn);
}