




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第Java數(shù)據(jù)庫連接池c3p0介紹詳細說明可參考官網(wǎng)文檔:/projects/c3p0/#configuration
2、原始連接操作
c3p0提供api來訪問原始連接中的非標準接口:
把連接轉(zhuǎn)成C3P0ProxyConnection
然后調(diào)用rawConnectionOperation方法
下面是獲取PostgreSQL
JDBC驅(qū)動中CopyManager對象的方法:
Connectionconnection=(C3P0ProxyConnection)c3p0DataSource.getConnection();
C3P0ProxyConnectioncastConnection=(C3P0ProxyConnection)connection;
Methodmethod=BaseConnection.class.getMethod("getCopyAPI",newClass[]{});
CopyManagercopyManager=(CopyManager)castConnection.rawConnectionOperation(method,C3P0ProxyConnection.RAW_CONNECTION,newObject[]{});
3、使用
3.1、直接使用
3.1.1、引入依賴
dependency
groupIdcom.mchange/groupId
artifactIdc3p0/artifactId
version/version
/dependency
3.1.2、使用例子
packagecom.abc.demo.general.dbpool;
importcom.mchange.v2.c3p0.ComboPooledDataSource;
importjava.beans.PropertyVetoException;
importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
publicclassC3p0Case{
publicstaticvoidmain(String[]args)throwsPropertyVetoException{
ComboPooledDataSourcecomboPooledDataSource=newComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://1:3306/mydbuseUnicode=truecharacterEncoding=UTF-8");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("123456");
comboPooledDataSource.setInitialPoolSize(2);
comboPooledDataSource.setMinPoolSize(2);
comboPooledDataSource.setMaxPoolSize(10);
comboPooledDataSource.setPreferredTestQuery("select1");
comboPooledDataSource.setIdleConnectionTestPeriod(60);
comboPooledDataSource.setTestConnectionOnCheckout(true);
comboPooledDataSource.setCheckoutTimeout(1000*30);
Connectionconnection=null;
Statementst=null;
ResultSetrs=null;
try{
connection=comboPooledDataSource.getConnection();
st=connection.createStatement();
rs=st.executeQuery("selectversion()");
if(rs.next()){
System.out.println(rs.getString(1));
}catch(SQLExceptione){
e.printStackTrace();
}finally{
close(connection);
//實際使用中一般是在應(yīng)用啟動時初始化數(shù)據(jù)源,應(yīng)用從數(shù)據(jù)源中獲取連接;并不會關(guān)閉數(shù)據(jù)源。
comboPooledDataSource.close();
privatestaticvoidclose(Connectionconnection){
if(connection!=null){
try{
connection.close();
}catch(SQLExceptione){
e.printStackTrace();
}
3.2、在SpringBoot中使用
3.1.1、引入依賴
parent
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-parent/artifactId
version2.3.12.RELEASE/version
relativePath/
/parent
dependencies
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-web/artifactId
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-jdbc/artifactId
/dependency
dependency
groupIdcom.mchange/groupId
artifactIdc3p0/artifactId
version/version
/dependency
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
/dependency
/dependencies
3.1.2、單數(shù)據(jù)源
application.yml配置:
spring:
datasource:
c3p0:
driver-class:com.mysql.cj.jdbc.Driver
jdbc-url:jdbc:mysql://1:3306/myDbuseUnicode=truecharacterEncoding=UTF-8
user:root
password:123456
initial-pool-size:2
min-pool-size:2
max-pool-size:10
preferred-test-query:select1
idle-connection-test-period:60
test-connection-on-checkout:true
checkout-timeout:30000
數(shù)據(jù)源配置類:
packagecom.abc.demo.config;
importcom.mchange.v2.c3p0.ComboPooledDataSource;
importperties.ConfigurationProperties;
importorg.springframework.boot.jdbc.DataSourceBuilder;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjavax.sql.DataSource;
@Configuration
publicclassDataSourceConfig{
@Bean("dataSource")
@ConfigurationProperties(prefix="spring.datasource.c3p0")
publicDataSourcedataSource1(){
returnDataSourceBuilder.create().type(ComboPooledDataSource.class).build();
}
使用:
@Autowired
privateDataSourcedataSource;
3.1.3、多數(shù)據(jù)源
application.yml配置:
spring:
datasource:
c3p0:
db1:
driver-class:com.mysql.cj.jdbc.Driver
jdbc-url:jdbc:mysql://1:3306/myDbuseUnicode=truecharacterEncoding=UTF-8
user:root
password:InsYR0ot187!
initial-pool-size:2
min-pool-size:2
max-pool-size:10
preferred-test-query:select1
idle-connection-test-period:60
test-connection-on-checkout:true
checkout-timeout:30000
db2:
driver-class:com.mysql.cj.jdbc.Driver
jdbc-url:jdbc:mysql://2:3306/myDbuseUnicode=truecharacterEncoding=UTF-8
user:root
password:InsYR0ot187!
initial-pool-size:2
min-pool-size:2
max-pool-size:10
preferred-test-query:select1
idle-connection-test-period:60
test-connection-on-checkout:true
checkout-timeout:30000
數(shù)據(jù)源配置類:
packagecom.abc.demo.config;
importcom.mchange.v2.c3p0.ComboPooledDataSource;
importperties.ConfigurationProperties;
importorg.springframework.boot.jdbc.DataSourceBuilder;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjavax.sql.DataSource;
@Configuration
publicclassDataSourceConfig{
@Bean("dataSource1")
@ConfigurationProperties(prefix="spring.datasource.c3p0.db1")
publicDataSourcedataSource1(){
returnDataSourceBuilder.create().type(ComboPooledDataSource.class).bui
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 城市交通規(guī)劃合同審查咨詢重點基礎(chǔ)知識點
- 通風(fēng)維保服務(wù)合同協(xié)議
- 軟件共同研發(fā)合同協(xié)議
- 轉(zhuǎn)讓房子租賃合同協(xié)議
- 小產(chǎn)權(quán)房買賣交易合同
- 轉(zhuǎn)讓非遺項目合同協(xié)議
- 輕鋼別墅承補充合同協(xié)議
- 民房粉刷協(xié)議書
- 運輸合伙協(xié)議合同協(xié)議
- 醫(yī)生醫(yī)師聘用勞動合同
- 跳繩市場調(diào)研報告
- 《大學(xué)生的情緒》課件
- 鐵道概論(第八版)佟立本主編
- 全國各省市名稱大全
- 202305青少年軟件編程(圖形化)等級考試試卷四級(含答案)
- 光儲充車棚技術(shù)方案設(shè)計方案
- 土壤重金屬源調(diào)查分析投標方案
- 植筋錨固深度表
- 幼兒園家長會會議記錄三篇
- 《心房顫動診斷和治療中國指南2023》解讀
- 樓頂瀝青澆灌施工方案
評論
0/150
提交評論