web項目都會使用數據庫,而數據庫訪問會影響系統性能(I/O操做)。創建數據庫鏈接是一個很是耗時耗資源的操做,經過配置鏈接池能夠在系統啓動時就分配並維護必定數量的鏈接,保持最低數量的鏈接數,經過設定鏈接池最大鏈接數來防止系統無盡的與數據庫鏈接。這樣在每次數據請求方須要鏈接時,直接從鏈接池獲取,使用完畢以後再放回去,這樣就能夠儘量減小沒必要要的數據庫鏈接消耗,緩解數據庫的訪問壓力,減輕對系統性能的影響。java
java經常使用的數據庫鏈接池主要有 DBCP 和 C3P0;mysql
DBCP(DataBase connection pool),數據庫鏈接池。是 apache 上的一個 Java 鏈接池項目,也是Tomcat 在 7.0 之前的版本使用的鏈接池組件。web
使用DBCP應該導入commons-dbcp-x.x.jar、commons-pool-x.x.jar、commons-logging-x.x.jarsql
commons-dbcp最新版本是Apache Commons DBCP 2.1.1 for JDBC 4.1 (Java 7.0+)數據庫
Apache Commons Pool 2.4.2 (Java 6.0+)apache
測試實例ide
TestDBCP.javaoop
package cn.iborder.utils; import java.io.BufferedInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSourceFactory; import org.junit.Test; public class TestDBCP { //硬編碼方式實現鏈接池 @Test public void test1() { Connection con = null; PreparedStatement pstat = null; ResultSet rs = null; BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost/test"); dataSource.setUsername("root"); dataSource.setPassword("root"); dataSource.setInitialSize(3); //dataSource.setMaxActive(6); //DBCP2.0以上版本已經移除此方法,由 setMaxTotal()代替 dataSource.setMaxTotal(6); //最大鏈接數 dataSource.setMaxIdle(5); //最大空閒鏈接數. dataSource.setMinIdle(4); try { con = dataSource.getConnection(); pstat = con.prepareStatement("select * from admin"); rs = pstat.executeQuery(); while (rs.next()) { System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(rs != null){ rs.close(); rs=null; } if(pstat != null){ pstat.close(); pstat=null; } if (con != null && !con.isClosed()) { con.close(); con=null; } } catch (Exception e2) { // TODO: handle exception } } } //配置文件方式實現鏈接池 @Test public void test2() { Connection con = null; PreparedStatement pstat = null; ResultSet rs = null; try { Properties prop = new Properties(); BufferedInputStream in = (BufferedInputStream) this.getClass().getResourceAsStream("/db.properties"); //FileInputStream in = (FileInputStream) TestDBCP.class.getResourceAsStream("/db.properties");//拋異常 prop.load(in); DataSource dataSource = BasicDataSourceFactory.createDataSource(prop); con = dataSource.getConnection(); pstat = con.prepareStatement("select * from admin"); rs = pstat.executeQuery(); while (rs.next()) { System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(rs != null){ rs.close(); rs=null; } if(pstat != null){ pstat.close(); pstat=null; } if (con != null && !con.isClosed()) { con.close(); con=null; } } catch (Exception e2) { // TODO: handle exception } } } }
db.properties性能
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost/test username=root password=root InitialSize=3 MaxTotal=6 MaxIdle=5 MinIdle=4
C3P0是一個開放源代碼的JDBC鏈接池,最經常使用的鏈接池技術,Spring/Hibernate框架默認支持C3P0
使用C3P0 應該導入c3p0.xxxx.jar和mchange-commons-java-x.x.x.jar
C3P0最新版本是C3P0 0.9.5.2
核心類:CombopooledDataSource
測試實例
TestC3P0.java
package cn.iborder.utils; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; public class TestC3P0 { //硬編碼方式實現鏈接池 @Test public void test1(){ Connection con = null; PreparedStatement pstat = null; ResultSet rs = null; try { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost/test"); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setInitialPoolSize(3); dataSource.setMaxPoolSize(6); dataSource.setMaxIdleTime(1000); con = dataSource.getConnection(); pstat = con.prepareStatement("select * from admin"); rs = pstat.executeQuery(); while (rs.next()) { System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)); } } catch (PropertyVetoException e) { // TODO: handle exception } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(rs != null){ rs.close(); rs=null; } if(pstat != null){ pstat.close(); pstat=null; } if (con != null && !con.isClosed()) { con.close(); con=null; } } catch (Exception e2) { // TODO: handle exception } } } //配置文件方式實現鏈接池 @Test public void test2() { Connection con = null; PreparedStatement pstat = null; ResultSet rs = null; try { //讀取默認數據庫 //ComboPooledDataSource dataSource = new ComboPooledDataSource(); //讀取指定name的數據庫 ComboPooledDataSource dataSource = new ComboPooledDataSource("otherDB"); con = dataSource.getConnection(); pstat = con.prepareStatement("select * from admin"); rs = pstat.executeQuery(); while (rs.next()) { System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(rs != null){ rs.close(); rs=null; } if(pstat != null){ pstat.close(); pstat=null; } if (con != null && !con.isClosed()) { con.close(); con=null; } } catch (Exception e2) { // TODO: handle exception } } } }
c3p0-config.xml
<c3p0-config> <!-- 默認數據庫 --> <default-config> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">3</property> <property name="maxPoolSize">6</property> <property name="maxIdleTime">1000</property> <user-overrides user="swaldman"> <!-- <property name="unreturnedConnectionTimeout">5</property> <property name="debugUnreturnedConnectionStackTraces">true</property> --> <!-- <property name="preferredTestQuery">select poop from doop</property> --> <!-- intentionally broken --> </user-overrides> </default-config> <!-- 配置其餘是數據庫 --> <named-config name="otherDB"> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">3</property> <property name="maxPoolSize">6</property> <property name="maxIdleTime">1000</property> </named-config> </c3p0-config>