您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
5_数据库连接池_c3p0_配置演示(连接池连接的归还及非默认连接池的使用)
发布时间:2022-07-20 22:23:13编辑:雪饮阅读()
同上篇一样,先贴出c3p0的配置:
c3p0-config.xml:
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<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">root</property>
<!-- 连接池参数 -->
<!--初始化连接数量-->
<property name="initialPoolSize">5</property>
<!--当前(该配置文件中可以有多个连接池配置)连接池最大连接数-->
<property name="maxPoolSize">10</property>
<!--
一个连接创建过程多久算超时(比如主机名填错了,那个这个连接一直创建不成功也不行,要配置一个创建连接超时时间)
(我觉得应该不仅仅只是指这个超时时间吧,可能也会影响到比如某个连接多久没有使用就自动回收之类的)
-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="otherc3p0">
<!-- 连接参数 -->
<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">root</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
然后是基于c3p0的不同配置加载以及连接回收等的实例:
PoolTest.java:
package day6;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class PoolTest {
public static void main(String args[]) throws SQLException {
getConncetionNoOverFlow();
getConncetionOverFlow();
getConncetionOverFlow2();
getConncetionNoOverFlow2();
getConncetionNoOverFlow3();
}
//这里使用默认连接池(当前配置最大连接是10),这里循环10次,正好用完,不会溢出
public static void getConncetionNoOverFlow() {
try{
DataSource ds=new ComboPooledDataSource();
for(int i=0;i<10;i++){
Connection conn=ds.getConnection();
System.out.println(conn);
}
}
catch(Exception e){
e.getStackTrace();
//在当前方法中获取方法名,Thread.currentThread().getStackTrace()[1].getMethodName()
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+"报错了:"+e.getMessage());
}
}
//这里使用默认连接池(当前配置最大连接是10),这里循环11次,就会溢出(由于默认连接池配置的checkoutTimeout是3000,所以第11个连接在获取3秒后超时)
public static void getConncetionOverFlow() {
try{
DataSource ds=new ComboPooledDataSource();
for(int i=0;i<11;i++){
Connection conn=ds.getConnection();
System.out.println(conn);
}
}
catch(Exception e){
e.getStackTrace();
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+"报错了:"+e.getMessage());
}
}
//可以通过ComboPooledDataSource传入字符串(连接池名称)则就可以从c3p0配置文件中以指定连接池的配置信息获取数据库连接,这里到第9个就获取不到了,因为当前配置这个otherc3p0连接池的maxPoolSize配置为8
public static void getConncetionOverFlow2() {
try{
DataSource ds=new ComboPooledDataSource("otherc3p0");
for(int i=0;i<9;i++){
Connection conn=ds.getConnection();
System.out.println(conn);
}
}
catch(Exception e){
e.getStackTrace();
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+"报错了:"+e.getMessage());
}
}
//同样的,在指定连接池中,则按照该连接池配置的maxPoolSize配置为8,则我们也最大获取8个,则就不会报错了
public static void getConncetionNoOverFlow2() {
try{
DataSource ds=new ComboPooledDataSource("otherc3p0");
for(int i=0;i<8;i++){
Connection conn=ds.getConnection();
System.out.println(conn);
}
}
catch(Exception e){
e.getStackTrace();
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+"报错了:"+e.getMessage());
}
}
//这里同样是在otherc3p0连接池中超过连接池最大连接数进行调用,但不会因为连接数超过最大连接数而报错,所谓的超时个人认为是等待池子里面其它连接的归还,
// 如果一直没有连接归还,则该连接池就一直空的,就一直等待,那么checkoutTimeout超时实际算是被你玩通了
//所以这里我们要在某些场合可以关闭某个连接(比如连接复用不太多的地方,当然这里要考虑连接重新建立以及连接闲置过久后被mysql踢掉的情况)
public static void getConncetionNoOverFlow3() {
try{
DataSource ds=new ComboPooledDataSource("otherc3p0");
for(int i=0;i<9;i++){
Connection conn=ds.getConnection();
System.out.println(conn);
//某些场合下
if(i==7){
//语法虽然是close,但是由于这里是使用了连接池,所以这里并非关闭连接,而是将该连接归还给了连接池
conn.close();
}
}
}
catch(Exception e){
e.getStackTrace();
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+"报错了:"+e.getMessage());
}
}
}
关键字词:java,连接池,c3p0,归还,非默认