MySQL---數(shù)據(jù)庫從入門走向大神系列(十四)-ComboPooledDataSource(C3P0連接池配置)

需要準備的jar包:

MySQL的jar包mysql-connector-java-5.1.39-bin目前是5.1.39版本:

http://dev.mysql.com/downloads/connector/j/

C3p0的2個包:

https://sourceforge.net/projects/c3p0/

c3p0-0.9.1.2.jar和c3p0-sources-0.9.1.2.jar

配置文件:c3p0-config.xml

名字必須為這個!

演示代碼:

1.package cn.hncu.C3p0;

2.?

3.import java.beans.PropertyVetoException;

4..import java.sql.Connection;

5..import java.sql.SQLException;

6.

7..import org.junit.Test;

8.

9..import com.mchange.v2.c3p0.ComboPooledDataSource;

10.

11..public class C3p0Demo {

12.

13.? ? ?@Test

14.? ? ?// 純Java方法使用c3p0

15.? ? public void C3p0Demo() throws PropertyVetoException, SQLException {

16.? ? ? ? ComboPooledDataSource pool = new ComboPooledDataSource();

17.? ? ? ? pool.setUser("root");// 用戶姓名

18.? ? ? ? pool.setPassword("1234");// 用戶密碼

19.? ? ? ? pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/hncu?seUnicode=true&characterEncoding=utf- 8");// MySQL數(shù)據(jù)庫連接url

20.? ? ? ? pool.setDriverClass("com.mysql.jdbc.Driver");

21.

22.? ? ? ? // pool可以調(diào)用set方法進行Connection池的配置

23.

24.

25.? ? ? ? // 連接關閉之后,內(nèi)存會被釋放,下次取時會重新開(內(nèi)存地址不共用)

26.? ? ? ? for (int i = 0; i < 20; i++) {

27.? ? ? ? ? ? Connection con = pool.getConnection();

28.? ? ? ? ? ? System.out.println(i + ":" + con);

29.? ? ? ? ? ? if (i % 2 == 0) {

30.? ? ? ? ? ? ? ? con.close();

31.? ? ? ? ? ? }

32.? ? ? ? }

33.? ? }

34.

35.? ? @Test

36.? ? // 演示采用配置文件的方式使用c3p0

37.? ? public void C3p0PropertyDemo() throws SQLException {

38.? ? ? ? ComboPooledDataSource pool = new ComboPooledDataSource();//空參,自動到classpath目錄下面加載“c3p0-config.xml”配置文件- -? ? ? ? ? ? ? ? ? ? ? ? ? -配置文件的存儲位置和名稱必須是這樣,且使用“默認配置”

39.? ? ? ? //ComboPooledDataSource pool = new ComboPooledDataSource("demo");//加載“c3p0-config.xml”文件中定義的“demo”這個配置元素

40.? ? ? ? ?for(int i=0;i<25;i++){

41.? ? ? ? ? ? ? ?Connection con = pool.getConnection();

42.? ? ? ? ? ? ? ?System.out.println(i+":"+ con.hashCode());

43.? ? ? ? ? ?}

44.? ? }

45.

46.}

包裝一下C3P0的Connection池

1. package cn.hncu.C3p0;

2.?

3. import java.sql.Connection;

4. import java.sql.SQLException;

5.

6. import javax.sql.DataSource;

7.

8. import com.mchange.v2.c3p0.ComboPooledDataSource;

9.

10. public class C3p0Pool {

11.? ? //我們的這個包裝,只是為了把c3p0池做成讓每個線程(客戶端)獲得的是同一個連接,方便做b/s框架下的事務

12.? ? private static DataSource pool;

13.? ?private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();

14.? ?static{

15.? ? ?pool = new ComboPooledDataSource("demo");

16.? ? ? //這里的參數(shù)視你的配置文件而定,也可以不寫,用默認的配置

17.? ? ?}

18.

19.? ? ?public static DataSource getDataSource(){

?20.? ? ? ? return pool;

?21.? ?}

22.

23.

24.? ? ?public static Connection getConnection() throws SQLException{

25.? ? ? ? Connection con = t.get();

26.? ? ? ? if(con==null){

27.? ? ? ? ? ? con=pool.getConnection();

28.? ? ? ? ? ? ?t.set(con);

29.? ? ? ? ?}

30.? ? ? ? return con;

31.? ? }

32. }

---------------------

作者:諳憶

來源:CSDN

原文:https://blog.csdn.net/qq_26525215/article/details/52212260

版權聲明:本文為博主原創(chuàng)文章,轉載請附上博文鏈接!

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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