dbcp和c3p0的使用

1.依賴
2.配置
3.編程

1.依賴包

dbcp:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

編程運行時可能會報錯
java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
只需要導入這個包:

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-juli -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-juli</artifactId>
    <version>8.5.0</version>
</dependency>

c3p0:

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>mchange-commons-java</artifactId>
    <version>0.2.3</version>
</dependency>

2.配置

都在項目的根目錄中

dbcp:

dbcp.properties

########DBCP配置文件##########
#驅動名
driverClassName=com.mysql.jdbc.Driver
#url
url=jdbc:mysql://127.0.0.1:3306/mydb
#用戶名
username=sa
#密碼
password=123456
#初試連接數
initialSize=30
#最大活躍數
maxTotal=30
#最大idle數
maxIdle=10
#最小idle數
minIdle=5
#最長等待時間(毫秒)
maxWaitMillis=1000
#程序中的連接不使用後是否被連接池回收(該版本要使用removeAbandonedOnMaintenance和removeAbandonedOnBorrow)
#removeAbandoned=true
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
#連接在所指定的秒數內未使用纔會被刪除(秒)(爲配合測試程序才配置爲1秒)
removeAbandonedTimeout=1

c3p0:

DriverClass = com.mysql.jdbc.Driver  
JdbcUrl = jdbc:mysql://localhost:3306/cloudhospital  
User = root  
Password = admin  
MaxPoolSize = 20  
MinPoolSize = 2  
InitialPoolSize = 5  
MaxStatements = 30  
MaxIdleTime = 100  

3.編程

dbcp:

創建一個DbcpUtils.java

package com.imooc.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory;

public class DbcpUtils {

    private static DataSource DS;

    private static final String configFile = "/dbcp.properties";


    public DbcpUtils(){
        initialDbcp();
    }

    /**
     * 初始化
     */
    private static void initialDbcp() {
        Properties props = new Properties();

        try {
            props.load(Object.class.getResourceAsStream(configFile));
            //使用DataSourceFactory創建一個DataSource
            DS = BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    /**
     * 從數據源獲取一個連接
     * @return
     */
    public  Connection getConn(){
        Connection con = null;
        if(DS != null){
            try {
                con= DS.getConnection();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                con.setAutoCommit(false);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return con;
        }
        return con;
    }

}

c3p0:

創建一個C3p0Utils.java

package com.imooc.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Utils {

    private static ComboPooledDataSource ds=new ComboPooledDataSource();

    private static final String configFile = "/c3p0.properties";

    /**
     * 初始化參數
     */
    static{
        Properties props = new Properties();

        try {
            props.load(C3p0Utils.class.getResourceAsStream(configFile));
            ds.setDriverClass(props.getProperty("DriverClass"));
            ds.setJdbcUrl(props.getProperty("JdbcUrl"));
            ds.setUser(props.getProperty("User"));
            ds.setPassword(props.getProperty("Password"));
            ds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
            ds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
            ds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
            ds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
            ds.setMaxIdleTime(Integer.parseInt(props.getProperty("MaxIdleTime")));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章