JDBC數(shù)據(jù)庫連接池 |
發(fā)布時間: 2012/8/23 16:51:43 |
數(shù)據(jù)庫創(chuàng)建鏈接是比較消耗資源的,訪問量非常高的情況會造成內(nèi)存溢出等等負(fù)面影響。 大部分?jǐn)?shù)據(jù)鏈接池解決了這個問題,一般的實現(xiàn)思路是這樣得: •鏈接池在初始化時首先獲取一定數(shù)量得鏈接并保存起來,程序請求鏈接時,直接由連接池返回一個,應(yīng)用程序使用完后再歸還給鏈接池。 常用的開源鏈接池:
使用需要導(dǎo)入兩個包
dbcp將數(shù)據(jù)庫配置相關(guān)的信息單獨存放在.properties配置文件中,我們只需要將dbcp示例中的.properties文件考入工程中。 dbcpconfig.propertes : 02 driverClassName=com.mysql.jdbc.Driver 03 url=jdbc:mysql://localhost:3306/day17 04 username=root 05 password=hang 06 07 #<!-- 初始化連接 --> 08 initialSize=10 09 10 #最大連接數(shù)量 11 maxActive=50 12 13 #<!-- 最大空閑連接 --> 14 maxIdle=20 15 16 #<!-- 最小空閑連接 --> 17 minIdle=5 18 19 #<!-- 超時等待時間以毫秒為單位 6000毫秒/1000等于60秒 --> 20 maxWait=60000 21 22 23 #JDBC驅(qū)動建立連接時附帶的連接屬性屬性的格式必須為這樣:[屬性名=property;] 24 #注意:"user" 與 "password" 兩個屬性會被明確地傳遞,因此這里不需要包含他們。 25 connectionProperties=useUnicode=true;characterEncoding=utf8 26 27 #指定由連接池所創(chuàng)建的連接的自動提交(auto-commit)狀態(tài)。 28 defaultAutoCommit=true 29 30 #driver default 指定由連接池所創(chuàng)建的連接的只讀(read-only)狀態(tài)。 31 #如果沒有設(shè)置該值,則“setReadOnly”方法將不被調(diào)用。(某些驅(qū)動并不支持只讀模式,如:Informix) 32 defaultReadOnly= 33 34 #driver default 指定由連接池所創(chuàng)建的連接的事務(wù)級別(TransactionIsolation)。 35 #可用值為下列之一:(詳情可見javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE 36 defaultTransactionIsolation=READ_COMMITTED JdbcUitls.java 02 03 //DataSource是DBCP的數(shù)據(jù)庫連接池 04 private static DataSource ds=null; 05 06 //既然是工具類,那就是拿來就能用的,不用new它 07 //這個靜態(tài)代碼塊確保其內(nèi)容只運行一次,這里在第一次調(diào)用的時候,獲取一個工廠 08 static{ 09 try{ 10 //讀取資源文件 11 Properties prop =new Properties(); 12 JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); 13 14 //DBCP的連接池工廠 15 BasicDataSourceFactory factory=new BasicDataSourceFactory(); 16 //通過這個工廠,我們獲得一個根據(jù)資源文件配置的數(shù)據(jù)庫連接池 17 ds = factory.createDataSource(prop); 18 19 }catch (Exception e) { 20 throw new RuntimeException(e); 21 } 22 } 23 24 //返回一個數(shù)據(jù)庫連接 25 public static Connection getConnection() throws SQLException{ 26 //從DataSource中獲取一個空閑得連接并返回給調(diào)用它的方法 27 return ds.getConnection(); 28 } 29 30 }
這樣我們在獲取連接的時候可以直接通過這個靜態(tài)類獲得,在使用完成后,保持良好的習(xí)慣去關(guān)閉這個連接,這個連接實質(zhì)上不會關(guān)閉,會被連接池收回,DBCP使用了動態(tài)代理來截斷調(diào)用者對Connection.close()方法得直接操作。
c3p0與dbcp一樣都是讀取配置文件
配置文件 c3p0-config.xml : 02 <c3p0-config> 03 04 <default-config> 05 06 <property name="driverClass">com.mysql.jdbc.Driver</property> 07 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb</property> 08 <property name="user">root</property> 09 <property name="password">root</property> 10 11 <property name="initialPoolSize">10</property> 12 13 <property name="maxIdleTime">30</property> 14 <property name="maxPoolSize">100</property> 15 <property name="minPoolSize">10</property> 16 <property name="maxStatements">200</property> 17 18 </default-config> 19 20 <-- named-config可以采取多個配置 --> 21 <named-config name="mysql"> 22 <property name="acquireIncrement">50</property> 23 24 <property name="initialPoolSize">100</property> 25 <property name="minPoolSize">50</property> 26 <property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching --> 27 <property name="maxStatements">0</property> 28 <property name="maxStatementsPerConnection">5</property> 29 30 </named-config> 31 32 33 34 </c3p0-config>
示例: 02 03 04 private static ComboPooledDataSource ds; 05 06 07 static{ 08 09 try{ 10 //這個是加載配置文件 11 12 ds = new ComboPooledDataSource(); 13 14 15 //這個手動設(shè)置,不加載配置文件 16 17 /*ds.setDriverClass("com.mysql.jdbc.Driver"); 18 19 ds.setJdbcUrl("jdbc:mysql://localhost:3306/day16"); 20 21 ds.setUser("root"); 22 23 ds.setPassword("root"); 24 25 ds.setInitialPoolSize(20);*/ 26 27 }catch (Exception e) { 28 29 throw new ExceptionInInitializerError(e); 30 31 } 32 33 } 34 35 36 37 public static Connection getConnection() throws SQLException{ 38 39 return ds.getConnection(); 本文出自:億恩科技【www.allwellnessguide.com】 服務(wù)器租用/服務(wù)器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM] |