Spring_11 c3p0 使用入門

c3p0 是一個開源的JDBC連接池,它實現(xiàn)了數(shù)據(jù)源和JNDI綁定,支持JDBC3規(guī)范和JDBC2的標(biāo)準擴展。目前使用它的開源項目有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

先導(dǎo)入基礎(chǔ)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

導(dǎo)入c3p0jar

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

代碼連接數(shù)據(jù)庫

ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
    //加載驅(qū)動
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    //設(shè)置數(shù)據(jù)庫的鏈接地址
    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 鏈接數(shù)據(jù)庫插入一條數(shù)據(jù)。

spring 配置文件中配置c3p0

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

下面的代碼是一個例子,在UserDao 中調(diào)用JdbcTemplate 向數(shù)據(jù)庫中插入一條數(shù)據(jù),然后又在UserService 中控制UserDao 中的插入動作,下面我們看看具體代碼如何實現(xiàn)。

  • 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" />
    <!--連接池中保留的最小連接數(shù)。 -->
    <property name="minPoolSize" value="10" />
    <!--連接池中保留的最大連接數(shù)。Default: 15 -->
    <property name="maxPoolSize" value="100" />
    <!--最大空閑時間,1800秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->
    <property name="maxIdleTime" value="1800" />
    <!--當(dāng)連接池中的連接耗盡的時候c3p0一次同時獲取的連接數(shù)。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" />
    <!--定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)。Default: 30 -->
    <property name="acquireRetryAttempts" value="30" />
    <property name="breakAfterAcquireFailure" value="true" />
    <property name="testConnectionOnCheckout" value="false" />
</bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,256評論 6 342
  • https://www.ibm.com/developerworks/cn/opensource/os-cn-sp...
    大同若魚閱讀 5,138評論 4 18
  • 一、 Spring技術(shù)概述1、什么是Spring : Spring是分層的JavaSE/EE full-stack...
    luweicheng24閱讀 776評論 0 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,628評論 18 399

友情鏈接更多精彩內(nèi)容