com.mchange.v2.c3p0.ComboPooledDataSource:数据库连接池的一种
C3P0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSources 对象。
1.需要的jar包:c3p0-0.9.1.2.jar
1 <dependency> 2 <groupId>c3p0</groupId> 3 <artifactId>c3p0</artifactId> 4 <version>0.9.1.2</version> 5 </dependency>
2.ComboPooledDataSource连接数据库方式:
(1)代码连接:
1 public class TestCombo{ 2 private ComboPooledDataSource dataSource; private TestCombo() { 3 try { 4 dataSource = new ComboPooledDataSource(); 5 dataSource.setUser(root); 6 dataSource.setPassword(123456); 7 dataSource.setJdbcUrl(jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8"); 8 dataSource.setDriverClass(com.mysql.jdbc.Driver); 9 dataSource.setInitialPoolSize(); 10 dataSource.setAcquireIncrement(); 11 dataSource.setMinPoolSize(); 12 dataSource.setMaxPoolSize(); 13 dataSource.setMaxStatements(); 14 dataSource.setMaxIdleTime(); 15 dataSource.setIdleConnectionTestPeriod(); 16 dataSource.setAcquireRetryAttempts(); 17 18 } catch (Exception e) { 19 throw new RuntimeException(e); 20 } 21 } 22 23 24 public Connection getConnection() { 25 try { 26 return dataSource.getConnection(); 27 } catch (Exception e) { 28 throw new RuntimeException("can not get sms database connection ", e); 29 } 30 } 31 32 } 33 --------------------- 34 作者:chenpuzhen 35 来源:CSDN 36 原文:https://blog.csdn.net/chenpuzhen/article/details/80610044 37 版权声明:本文为博主原创文章,转载请附上博文链接!
(2)配置文件连接:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" > <property name="driverClass" value="${database.driver}" /> <property name="jdbcUrl" value="${database.url}" /> <property name="user" value="${database.username}" /> <property name="password" value="${database.password}" /> <property name="loginTimeout" value="3000"></property> <!--checkoutTimeout从池中拿未使用的连接,超时设置,如果没有设置,就不超时. --> <property name="checkoutTimeout" value="30000"></property> <property name="preferredTestQuery" value="select 1 from dual"></property> <!--每18000秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="18000"></property> <!--testConnectionOnCheckout:如果设置为true,每次从池中取一个连接,将做一下测试,使用automaticTestTable 或者 preferredTestQuery,做一条查询语句.看看连接好不好用,不好用,就关闭它,重新从池中拿一个. --> <property name="testConnectionOnCheckout" value="true"></property> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="5"></property> <!--连接池中保留的最大连接数。-->
<property name="maxPoolSize" value="100"></property> <!-- 初始化池大小 --> <property name="initialPoolSize" value="10"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。 --> <property name="acquireIncrement" value="5"></property> <!--从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="30"></property> <property name="acquireRetryDelay" value="1000"></property> <!--最大空闲时间,3600秒内未使用则连接被丢弃。若为0则永不丢弃。--> <property name="maxIdleTime" value="3600"></property> </bean> <!--另一数据库连接 --> <bean id="dataSourceQuas" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" > <property name="driverClass" value="${quas.database.driver}" /> <property name="jdbcUrl" value="${quas.database.url}" /> <property name="user" value="${quas.database.username}" /> <property name="password" value="${quas.database.password}" /> <property name="loginTimeout" value="3000"></property> <property name="checkoutTimeout" value="30000"></property> <property name="preferredTestQuery" value="select 1 from dual"></property> <property name="idleConnectionTestPeriod" value="18000"></property> <property name="testConnectionOnCheckout" value="true"></property> <property name="minPoolSize" value="5"></property> <property name="maxPoolSize" value="100"></property> <property name="initialPoolSize" value="10"></property> <property name="acquireIncrement" value="5"></property> <property name="acquireRetryAttempts" value="30"></property> <property name="acquireRetryDelay" value="1000"></property> <property name="maxIdleTime" value="3600"></property> </bean> <bean id="dataSourceCtps" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" > <property name="driverClass" value="${ctps.database.driver}" /> <property name="jdbcUrl" value="${ctps.database.url}" /> <property name="user" value="${ctps.database.username}" /> <property name="password" value="${ctps.database.password}" /> <property name="loginTimeout" value="3000"></property> <property name="checkoutTimeout" value="30000"></property> <property name="preferredTestQuery" value="select 1 from dual"></property> <property name="idleConnectionTestPeriod" value="18000"></property> <property name="testConnectionOnCheckout" value="true"></property> <property name="minPoolSize" value="5"></property> <property name="maxPoolSize" value="100"></property> <property name="initialPoolSize" value="10"></property> <property name="acquireIncrement" value="5"></property> <property name="acquireRetryAttempts" value="30"></property> <property name="acquireRetryDelay" value="1000"></property> <property name="maxIdleTime" value="3600"></property> </bean> </beans>
(3)资源文件设置数据库相关值:database.properties