C3P0连接池

来源:互联网 发布:多媒体管理系统 源码 编辑:程序博客网 时间:2024/06/09 14:58

C3P0连接池:
最常用的连接池技术!Spring框架,默认支持C3P0连接池技术!
C3P0连接池,核心类:
CombopooledDataSource ds;

jar:
c3p0-0.9.1.2.jar

硬编码:

    @Test    public void testCode() throws Exception {        // 创建连接池核心工具类        ComboPooledDataSource dataSource = new ComboPooledDataSource();        // 设置连接参数:url、驱动、用户密码、初始连接数、最大连接数        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_demo");        dataSource.setDriverClass("com.mysql.jdbc.Driver");        dataSource.setUser("root");        dataSource.setPassword("root");        dataSource.setInitialPoolSize(3);        dataSource.setMaxPoolSize(6);        dataSource.setMaxIdleTime(1000);        // ---> 从连接池对象中,获取连接对象        Connection con = dataSource.getConnection();        // 执行更新        con.prepareStatement("delete from admin where id=7").executeUpdate();        // 关闭        con.close();    }

xml配置:

    @Test    public void testXML() throws Exception {        // 创建c3p0连接池核心工具类        // 自动加载src下c3p0的配置文件【c3p0-config.xml】        ComboPooledDataSource dataSource = new ComboPooledDataSource();// 使用默认的配置        //有参数的情况是对用xml文件的名字        // 获取连接        Connection con = dataSource.getConnection();        // 执行更新        con.prepareStatement("delete from admin where id=5").executeUpdate();        // 关闭        con.close();    }}

xml文件:

<c3p0-config>    <default-config>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo        </property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="user">root</property>        <property name="password">root</property>        <property name="initialPoolSize">3</property>        <property name="maxPoolSize">6</property>        <property name="maxIdleTime">1000</property>    </default-config>    <named-config name="oracle_config">        <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:jdbc_demo</property>        <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>        <property name="user">root</property>        <property name="password">root</property>        <property name="initialPoolSize">3</property>        <property name="maxPoolSize">6</property>        <property name="maxIdleTime">1000</property>    </named-config></c3p0-config>
0 0
原创粉丝点击