您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)c3p0數(shù)據(jù)庫連接池如何使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
一、jar包
引用開源項(xiàng)目,自然要先下載人家的jar包,我這里有一個(gè)在云盤:c3p0-0.9.5-pre4.jar,這里面其實(shí)有三個(gè)包,是我在開源中國下載的最新的,如果你還想要更新的不妨自己到網(wǎng)上去搜一下。
二、配置文件
作為一個(gè)數(shù)據(jù)庫連接池自然有很多參數(shù)要設(shè)置,當(dāng)然就算你不設(shè)置也有默認(rèn)的,不過那不一定能滿足你的要求。這里的配置文件沒有什么特別的要求,可以是xml也可以是properties甚至與txt都行,當(dāng)然如果你愿意也可以寫死在程序中。
下面是我的配置文件,我把它放在了src目錄下,名字叫“c3p0.properties”
[java] view
plaincopy
#jdbc基本信息
driverClass=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521/orcl
user=bctts
password=bctts123
#c3p0連接池信息
c3p0.minPoolSize=3
c3p0.maxPoolSize=25
#當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)
c3p0.acquireIncrement=3
#定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)
c3p0.acquireRetryAttempts=60
#兩次連接中間隔時(shí)間,單位毫秒
c3p0.acquireRetryDelay=1000
#連接關(guān)閉時(shí)默認(rèn)將所有未提交的操作回滾
c3p0.autoCommitOnClose=false
#當(dāng)連接池用完時(shí)客戶端調(diào)用getConnection()后等待獲取新連接的時(shí)間,超時(shí)后將拋出SQLException,如設(shè)為0則無限期等待。單位毫秒
c3p0.checkoutTimeout=3000
#每120秒檢查所有連接池中的空閑連接。Default: 0
c3p0.idleConnectionTestPeriod=120
#最大空閑時(shí)間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0
c3p0.maxIdleTime=600
#如果設(shè)為true那么在取得連接的同時(shí)將校驗(yàn)連接的有效性。Default: false
c3p0.testConnectionOnCheckin=true
#c3p0將建一張名為c3p0TestTable的空表,并使用其自帶的查詢語句進(jìn)行測試。
jdbc.automaticTestTable = c3p0TestTable
配置文件中的內(nèi)容我就不解釋了,相信你看一眼就能很明白。
三、代碼實(shí)現(xiàn)
c3p0的使用非常簡單,基本上就是三步搞定。
DataSource unPooled = DataSources.unpooledDataSource(url,jdbcproperties);//這里的第二個(gè)參數(shù)放的是連接數(shù)據(jù)庫的信息,包括user、password等信息
DataSource ds = DataSources.pooledDataSource(unPooled,c3propertis);//這里面的第二個(gè)參數(shù)放的是c3p0連接池的配置信息
Connection conn = ds.getConnection();
這三句代碼相信大家很容易看明白,也是使用c3p0連接池的核心代碼,真的很簡單。
下面是我寫的例子,僅供參考
[java] view
plaincopy
package com.bks.db;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources;
/**
* c3p0連接池管理類
* @author ICE
*
*/
public class C3P0ConnentionProvider {
private static final String JDBC_DRIVER = "driverClass";
private static final String JDBC_URL = "jdbcUrl";
private static DataSource ds;
/**
* 初始化連接池代碼塊
*/
static{
initDBSource();
}
/**
* 初始化c3p0連接池
*/
private static final void initDBSource(){
Properties c3p0Pro = new Properties();
try {
//加載配置文件
c3p0Pro.load(new FileInputStream("./bin/c3p0.properties"));
} catch (Exception e) {
e.printStackTrace();
}
String drverClass = c3p0Pro.getProperty(JDBC_DRIVER);
if(drverClass != null){
try {
//加載驅(qū)動(dòng)類
Class.forName(drverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Properties jdbcpropes = new Properties();
Properties c3propes = new Properties();
for(Object key:c3p0Pro.keySet()){
String skey = (String)key;
if(skey.startsWith("c3p0.")){
c3propes.put(skey, c3p0Pro.getProperty(skey));
}else{
jdbcpropes.put(skey, c3p0Pro.getProperty(skey));
}
}
try {
//建立連接池
DataSource unPooled = DataSources.unpooledDataSource(c3p0Pro.getProperty(JDBC_URL),jdbcpropes);
ds = DataSources.pooledDataSource(unPooled,c3propes);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 獲取數(shù)據(jù)庫連接對(duì)象
* @return 數(shù)據(jù)連接對(duì)象
* @throws SQLException
*/
public static synchronized Connection getConnection() throws SQLException{
final Connection conn = ds.getConnection();
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
return conn;
}
}
下面這個(gè)是測試的例子
[java] view
plaincopy
package test.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.bks.db.C3P0ConnentionProvider;
public class Demo {
public static void main(String[] args) {
for(int i=0;i<30;i++){
new Thread(new Demo2()).start();
}
}
}
class Demo2 implements Runnable{
public void run(){
try {
Connection conn = C3P0ConnentionProvider.getConnection();
PreparedStatement pst = conn.prepareStatement("select to_char(sysdate,'hh34:mm:ss') tim from dual");
ResultSet rs = pst.executeQuery();
rs.next();
System.out.println(Thread.currentThread().getName()+":"+rs.getString(1));
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
上述就是小編為大家分享的c3p0數(shù)據(jù)庫連接池如何使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。