您好,登錄后才能下訂單哦!
package com.hanchao.test; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import javax.sql.DataSource; import com.mchange.v2.c3p0.DataSources; /*********************** * @author:han * @version:1.0 * @created:2015-10-11 *********************** */ public class TestPool { public static void main(String[] args) { /* Scanner input = new Scanner(System.in); System.out.println("請輸入賬號:"); String name = input.next(); System.out.println("請輸入密碼:"); String pwd = input.next(); System.out.println("請輸入金額:"); float money = input.nextFloat();*/ final String DRIVER = "com.mysql.jdbc.Driver"; //final String URL = "jdbc:mysql://127.0.0.1:3306/mydb"; //final String URL = "jdbc:mysql://localhost:3306/mydb"; final String URL = "jdbc:mysql:///mydb"; final String NAME = "root"; final String PASSWORD = "root"; Connection conn = null; Statement stat = null; ResultSet rs = null; try { //1.加載數(shù)據(jù)庫驅(qū)動(dòng) Class.forName(DRIVER); //連接mysql數(shù)據(jù)庫 DataSource unpooled = DataSources.unpooledDataSource(URL,NAME,PASSWORD); //構(gòu)建一個(gè)連接池 DataSource pooled = DataSources.pooledDataSource(unpooled); //2.獲取數(shù)據(jù)庫連接(first Time) conn = pooled.getConnection(); System.out.println("1 con Class Type is :" + conn.getClass().getName()); //取得內(nèi)部的實(shí)際數(shù)據(jù)庫連接 Object o1 = getInner(conn); System.out.println("1 Inner con Class Type is :" + o1.getClass().getName()); //3.獲取Statement對象 stat = conn.createStatement(); //4.執(zhí)行SQL rs = stat.executeQuery(" select * from t_user where id = 13"); while (rs.next()) { System.out.println(" username:" + rs.getString("username")); } //5.關(guān)閉連接 rs.close(); stat.close(); conn.close(); //6.等待連接返回池中 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //第二次獲取數(shù)據(jù)庫連接 conn = pooled.getConnection(); System.out.println("2 con Class Type is :" + conn.getClass().getName()); Object o2 = getInner(conn); System.out.println("2 Inner con Class Type is :" + o2.getClass().getName()); //獲取Statement對象 stat = conn.createStatement(); //3.獲取Statement對象 stat = conn.createStatement(); //4.執(zhí)行SQL rs = stat.executeQuery(" select * from t_user where id = 13"); while (rs.next()) { System.out.println(" username:" + rs.getString("username")); } /* //3.獲取Statement對象 // String sql = "delete from t_user where id = 13"; // String sql = "insert into t_user(username,address) values('tom1','USA1')"; StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO t_account(username,`password`,money,`enable`) "); sql.append("VALUES "); sql.append("('"+name+"','"+pwd+"','"+money+"','1')"); System.out.println("SQL:" + sql.toString()); //執(zhí)行SQL // int rows = stat.executeUpdate(sql); int rows = stat.executeUpdate(sql.toString()); if (rows > 0) { System.out.println("execute OK!!"); } else { System.out.println("execute error !!"); } */ } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stat != null) { stat.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } private static Object getInner(Object conn) { Object object = null; Field f ; try { f = conn.getClass().getDeclaredField("inner"); f.setAccessible(true); object = f.get(conn); f.setAccessible(false); } catch (Exception e) { e.printStackTrace(); } return object; } }
結(jié)果:
首先,從數(shù)據(jù)庫連接池獲得一個(gè)連接。發(fā)現(xiàn)連接類型并不是mysql的數(shù)據(jù)庫連接,而是,com.mchange.v2.c3p0.impl.NewProxyConnection。通過類名,可以推測,從數(shù)據(jù)庫連接池中獲取的只是一個(gè)代理。
當(dāng)我們關(guān)閉.NewProxyConnection連接時(shí),并沒有真正關(guān)閉連接,而只是將數(shù)據(jù)庫連接放入連接池保存,使得數(shù)據(jù)庫連接在連接池中得到復(fù)用。而從連接池返回的NewProxyConnection對象,只是對真實(shí)數(shù)據(jù)庫連接的包裝。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。