当前位置: 移动技术网 > IT编程>开发语言>Java > C3P0连接池的使用

C3P0连接池的使用

2019年05月24日  | 移动技术网IT编程  | 我要评论

c3p0是一个开源的jdbc连接池,它实现了数据源和jndi绑定,支持jdbc3规范和jdbc2的标准扩展,设计非常简单易用

c3p0的基本使用

添加maven依赖

 <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>5.1.44</version>
        </dependency>
        <dependency>
            <groupid>com.mchange</groupid>
            <artifactid>c3p0</artifactid>
            <version>0.9.5</version>
        </dependency>

编写c3p0工具类

public class c3p0utils {
    private static combopooleddatasource datasource = new combopooleddatasource("mysql");

    public static datasource getdatasource() {
        return datasource;
    }

    public static connection getconnection() {
        try {
            return datasource.getconnection();
        } catch (sqlexception e) {
            throw new runtimeexception(e);
        }
    }

    //释放连接回连接池
    public static void close(connection conn, preparedstatement pst, resultset rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
        }
        if (pst != null) {
            try {
                pst.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (sqlexception e) {
                e.printstacktrace();
            }
        }
    }
}

classpath下面配置文件
c3p0-config.xml

<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>

    <default-config>
        <property name="driverclass">com.mysql.jdbc.driver</property>
        <property name="jdbcurl">jdbc:mysql://192.168.47.151:3306/web</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialpoolsize">5</property>
        <property name="maxpoolsize">20</property>
    </default-config>

    <named-config name="mysql">
        <property name="driverclass">com.mysql.jdbc.driver</property>
        <property name="jdbcurl">jdbc:mysql://192.168.47.151:3306/web</property>
        <property name="user">root</property>
        <property name="password">root</property>
    </named-config>


</c3p0-config>

测试代码

 @test
    public void test1(){
        connection conn = null;
        preparedstatement pstmt = null;
        // 1.创建自定义连接池对象
        datasource datasource = new datasourcepool();
        try {
            // 2.从池子中获取连接
            conn = c3p0utils.getconnection();
            string sql = "insert into user values(?,?)";
            //3.必须在自定义的connection类中重写preparestatement(sql)方法
            pstmt = conn.preparestatement(sql);
            pstmt.setstring(1, "李四");
            pstmt.setstring(2, "1234");
            int rows = pstmt.executeupdate();
            system.out.println("rows:"+rows);
        } catch (exception e) {
            throw new runtimeexception(e);
        } finally {
            c3p0utils.close(conn,pstmt,null);
        }
    }

c3p0的其他细化配置

<!--acquireincrement:链接用完了自动增量3个。 -->
    <property name="acquireincrement">3</property>

    <!--acquireretryattempts:链接失败后重新试30次。-->
    <property name="acquireretryattempts">30</property>
 
    <!--acquireretrydelay;两次连接中间隔1000毫秒。 -->
    <property name="acquireretrydelay">1000</property>
 
    <!--autocommitonclose:连接关闭时默认将所有未提交的操作回滚。 -->
    <property name="autocommitonclose">false</property>
 
    <!--automatictesttable:c3p0测试表,没什么用。-->
    <property name="automatictesttable">test</property>
 
    <!--breakafteracquirefailure:出错时不把正在提交的数据抛弃。-->
    <property name="breakafteracquirefailure">false</property>
 
    <!--checkouttimeout:100毫秒后如果sql数据没有执行完将会报错,如果设置成0,那么将会无限的等待。 --> 
    <property name="checkouttimeout">100</property>
 
    <!--connectiontesterclassname:通过实现connectiontester或queryconnectiontester的类来测试连接。类名需制定全路径。default: com.mchange.v2.c3p0.impl.defaultconnectiontester-->
    <property name="connectiontesterclassname"></property>
 
    <!--factoryclasslocation:指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可。-->
    <property name="factoryclasslocation">null</property>
 
    <!--forceignoreunresolvedtransactions:作者强烈建议不使用的一个属性。--> 
    <property name="forceignoreunresolvedtransactions">false</property>
 
    <!--idleconnectiontestperiod:每60秒检查所有连接池中的空闲连接。--> 
    <property name="idleconnectiontestperiod">60</property>
 
    <!--initialpoolsize:初始化时获取三个连接,取值应在minpoolsize与maxpoolsize之间。 --> 
    <property name="initialpoolsize">3</property>
 
    <!--maxidletime:最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。-->
    <property name="maxidletime">60</property>
 
    <!--maxpoolsize:连接池中保留的最大连接数。 -->
    <property name="maxpoolsize">15</property>
 
    <!--maxstatements:最大链接数。-->
    <property name="maxstatements">100</property>
 
    <!--maxstatementsperconnection:定义了连接池内单个连接所拥有的最大缓存statements数。default: 0  -->
    <property name="maxstatementsperconnection"></property>
 
    <!--numhelperthreads:异步操作,提升性能通过多线程实现多个操作同时被执行。default: 3--> 
    <property name="numhelperthreads">3</property>
 
    <!--overridedefaultuser:当用户调用getconnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0的数据源时。default: null--> 
    <property name="overridedefaultuser">root</property>
 
    <!--overridedefaultpassword:与overridedefaultuser参数对应使用的一个参数。default: null-->
    <property name="overridedefaultpassword">password</property>
 
    <!--password:密码。default: null--> 
    <property name="password"></property>
 
    <!--preferredtestquery:定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意: 测试的表必须在初始数据源的时候就存在。default: null-->
    <property name="preferredtestquery">select id from test where id=1</property>
 
    <!--propertycycle:用户修改系统配置参数执行前最多等待300秒。default: 300 --> 
    <property name="propertycycle">300</property>
 
    <!--testconnectiononcheckout:因性能消耗大请只在需要的时候使用它。default: false -->
    <property name="testconnectiononcheckout">false</property>
 
    <!--testconnectiononcheckin:如果设为true那么在取得连接的同时将校验连接的有效性。default: false -->
    <property name="testconnectiononcheckin">true</property>
 
    <!--user:用户名。default: null-->
    <property name="user">root</property>
 
    <!--usestraditionalreflectiveproxies:动态反射代理。default: false-->
    <property name="usestraditionalreflectiveproxies">false</property>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网