美文网首页
Spring_11 c3p0 使用入门

Spring_11 c3p0 使用入门

作者: mm_cuckoo | 来源:发表于2017-10-06 18:03 被阅读75次

    c3p0 是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。

    在Spring 中引入c3p0

    下载c3p0

    c3p0 主页:http://www.mchange.com/projects/c3p0/
    c3p0下载地址:https://sourceforge.net/projects/c3p0/files/latest/download?source=typ_redirect

    引入jar

    先导入基础jar

    包名
    commons-logging-1.1.3.jar
    log4j-1.2.17.jar
    spring-beans-4.2.4.RELEASE.jar
    spring-context-4.2.4.RELEASE.jar
    spring-core-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar
    spring-aop-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar
    aopalliance-1.0.jar
    aspectjweaver-1.8.9.jar
    spring-aspects-4.2.4.RELEASE
    spring-jdbc-4.2.4.RELEASE.jar
    spring-tx-4.2.4.RELEASE.jar

    导入c3p0jar

    包名
    c3p0-0.9.5.2.jar
    mchange-commons-java-0.2.11.jar

    代码连接数据库

    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    try {
        //加载驱动
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        //设置数据库的链接地址
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/spring_db");
        //用户名
        dataSource.setUser("root");
        //密码
        dataSource.setPassword("root");
    } catch (PropertyVetoException e) {
        e.printStackTrace();
    }
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "INSERT INTO user_info VALUES(?,?)";
    int row  = jdbcTemplate.update(sql, "zhangwu", 200);
    System.out.println(row);
    

    上面代码是用c3p0 链接数据库插入一条数据。

    spring 配置文件中配置c3p0

    在Spring 中XML 中配置c3p0 原理就是依赖注入(IOC)。

    下面的代码是一个例子,在UserDao 中调用JdbcTemplate 向数据库中插入一条数据,然后又在UserService 中控制UserDao 中的插入动作,下面我们看看具体代码如何实现。

    • UserDao
    public class UserDao {
        public JdbcTemplate jdbcTemplate;
        
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        public void  addUser() {
            String sql = "INSERT INTO user_info VALUES(?,?)";
            int row  = jdbcTemplate.update(sql, "xiaoxin", 20171006);
            System.out.println(row);
        }
    }
    
    
    • UserService
    public class UserService {
        public UserDao userDao;
        
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public void  add() {
            userDao.addUser();
        }
    }
    
    
    • spring.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
            //配置c3p0 
        <bean id="cPDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring_db" />
            <property name="user" value="root" />
            <property name="password" value="root" />
        </bean>
            // 将c3p0 注入JdbcTemplate
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="cPDataSource" />
        </bean>
            //将UserDao 注入UserService
        <bean id="userService" class="com.cfox.spring.UserService">
            <property name="userDao" ref="userDao" />
        </bean>
            //将JdbcTemplate 注入UserDao
        <bean id="userDao" class="com.cfox.spring.UserDao">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>
    </beans>
    

    c3p0 的一些配置

    <bean id="cPDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring_db" />
        <property name="user" value="root" />
        <property name="password" value="root" />
        <!--连接池中保留的最小连接数。 -->
        <property name="minPoolSize" value="10" />
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="100" />
        <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800" />
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3" />
        <property name="maxStatements" value="1000" />
        <property name="initialPoolSize" value="10" />
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod" value="60" />
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="30" />
        <property name="breakAfterAcquireFailure" value="true" />
        <property name="testConnectionOnCheckout" value="false" />
    </bean>
    

    相关文章

      网友评论

          本文标题:Spring_11 c3p0 使用入门

          本文链接:https://www.haomeiwen.com/subject/jnjryxtx.html