MySql資料庫連接池,mysql資料庫連接

來源:互聯網
上載者:User

MySql資料庫連接池,mysql資料庫連接

1.傳統連結(如下為)


注意:

(1).傳統方式找DriverManager要串連,數目是有限的。

(2).傳統方式的close(),並沒有將Connection重用,只是切斷應用程式和資料庫的橋樑,即無發送到SQL命令到資料庫端執行 

(3).項目中,對於Connection不說,不會直接使用DriverManager取得,而使用串連池方式。


2.採用串連池()


3.開來源資料庫串連池

(1).現在很多Web伺服器(Weblogic, WebSphere, Tomcat)都提供了DataSoruce的實現,即串連池的實現。通常我們把DataSource的實現,按其英文含義稱之為資料來源,資料來源中都包含了資料庫連接池的實現。
(2).也有一些開源組織提供了資料來源的獨立實現:
       DBCP 資料庫連接池(tomcat)
       C3P0 資料庫連接池(hibernate)
(3).實際應用時不需要編寫串連資料庫代碼,直接從資料來源獲得資料庫的串連。程式員編程時也應盡量使用這些資料來源的實現,以提升程式的資料庫訪問效能。

4.C3P0的使用:
(1).在classpath的路徑下配置xml檔案,即src的檔案下而且這個檔案名稱必須是c3p0-config.xml

原始的c3p0-config.xml的檔案參數:

<c3p0-config><default-config><!-- 當串連池中的串連耗盡的時候c3p0一次同時擷取的串連數。Default: 3 --><property name="acquireIncrement">3</property><!-- 定義在從資料庫擷取新串連失敗後重複嘗試的次數。Default: 30 --><property name="acquireRetryAttempts">30</property><!-- 兩次串連中間隔時間,單位毫秒。Default: 1000 --><property name="acquireRetryDelay">1000</property><!-- 串連關閉時預設將所有未提交的操作復原。Default: false --><property name="autoCommitOnClose">false</property><!--c3p0將建一張名為Test的空表,並使用其內建的查詢語句進行測試。如果定義了這個參數那麼屬性preferredTestQuery將被忽略。你不能在這張Test表上進行任何操作,它將只供c3p0測試 使用。Default:null--><property name="automaticTestTable">Test</property><!--擷取串連失敗將會引起所有等待串連池來擷取串連的線程拋出異常。但是資料來源仍有效保留,並在下次調用getConnection()的時候繼續嘗試擷取串連。如果設為true,那麼在嘗試擷取串連失敗後該資料來源將申明已斷開並永久關閉。Default: false--><property name="breakAfterAcquireFailure">false</property><!--當串連池用完時用戶端調用getConnection()後等待擷取新串連的時間,逾時後將拋出SQLException,如設為0則無限期等待。單位毫秒。Default: 0--><property name="checkoutTimeout">100</property><!--通過實現ConnectionTester或QueryConnectionTester的類來測試連接。類名需制定全路徑。 Default:com.mchange.v2.c3p0.impl.DefaultConnectionTester--><property name="connectionTesterClassName"></property><!--指定c3p0 libraries的路徑,如果(通常都是這樣)在本地即可獲得那麼無需設定,預設null即可 Default: null--><property name="factoryClassLocation">null</property><!--Strongly disrecommended. Setting this to true may lead to subtle andbizarre bugs. (文檔原文)作者強烈建議不使用的一個屬性--><property name="forceIgnoreUnresolvedTransactions">false</property><!-- 每60秒檢查所有串連池中的空閑串連。Default: 0 --><property name="idleConnectionTestPeriod">60</property><!-- 初始化時擷取三個串連,取值應在minPoolSize與maxPoolSize之間。Default: 3 --><property name="initialPoolSize">3</property><!-- 最大空閑時間,60秒內未使用則串連被丟棄。若為0則永不丟棄。Default: 0 --><property name="maxIdleTime">60</property><!-- 串連池中保留的最大串連數。Default: 15 --><property name="maxPoolSize">15</property><!--JDBC的標準參數,用以控制資料來源內載入的PreparedStatements數量。但由於預緩衝的statements屬於單個connection而不是整個串連池。所以設定這個參數需要考慮到多方面的因素。如果maxStatements與maxStatementsPerConnection均為0,則緩衝被關閉。Default: 0--><property name="maxStatements">100</property><!--maxStatementsPerConnection定義了串連池內單個串連所擁有的最大緩衝statements數。Default: 0--><property name="maxStatementsPerConnection"></property><!--c3p0是非同步作業的,緩慢的JDBC操作通過協助進程完成。擴充這些操作可以有效提升效能通過多線程實現多個操作同時被執行。Default: 3--><property name="numHelperThreads">3</property><!--當使用者調用getConnection()時使root使用者成為去擷取串連的使用者。主要用於串連池串連非c3p0 的資料來源時。Default:null--><property name="overrideDefaultUser">root</property><!-- 與overrideDefaultUser參數對應使用的一個參數。Default: null--><property name="overrideDefaultPassword">password</property><!-- 密碼。Default: null--><property name="password"></property><!--定義所有串連測試都執行的測試語句。在使用串連測試的情況下這個一顯著提高測試速度。注意:測試的表必須在初始資料來源的時候就存在。Default: null--><property name="preferredTestQuery">select id from test where id=1</property><!-- 使用者修改系統配置參數執行前最多等待300秒。Default: 300 --><property name="propertyCycle">300</property><!--因效能消耗大請只在需要的時候使用它。如果設為true那麼在每個connection提交的時候都將校正其有效性。建議使用idleConnectionTestPeriod或automaticTestTable等方法來提升串連測試的效能。Default: false--><property name="testConnectionOnCheckout">false</property><!-- 如果設為true那麼在取得串連的同時將校正串連的有效性。Default: false --><property name="testConnectionOnCheckin">true</property><!-- 使用者名稱。Default: null--><property name="user">root</property><!--早期的c3p0版本對JDBC介面採用動態反射代理。在早期版本用途廣泛的情況下這個參數允許使用者恢複到動態反射代理以解決不穩定的故障。最新的非反射代理更快並且已經開始廣泛的被使用,所以這個參數未必有用。現在原先的動態反射與新的非反射代理同時受到支援,但今後可能的版本可能不支援動態反射代理。Default:false。--><property name="usesTraditionalReflectiveProxies">false</property><property name="automaticTestTable">con_test</property><property name="checkoutTimeout">30000</property><property name="idleConnectionTestPeriod">30</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">25</property><property name="minPoolSize">10</property><property name="maxStatements">0</property><user-overrides user="swaldman"></user-overrides></default-config><named-config name="dumbTestConfig"><property name="maxStatements">200</property><user-overrides user="poop"><property name="maxStatements">300</property></user-overrides></named-config></c3p0-config>

實戰練習:
1>.首先配置c3p0-config.xml檔案

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="user">root</property><property name="password">wwh</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/jdbctest</property></default-config></c3p0-config>

2>.代碼操作串連(TestC3P0.java)

package cn.wwh.www.java.jdbc.datasource;import java.sql.Connection;import com.mchange.v2.c3p0.ComboPooledDataSource;/** *類的作用: * * *@author 一葉扁舟 *@version 1.0 *@建立時間: 2014-9-2   上午12:02:13 *///測試連接池C3P0的用法public class TestC3P0 {public static void main(String[] args) throws Exception {long begin = System.currentTimeMillis();//建立C3P0串連池,並載入c3p0-config.xml檔案ComboPooledDataSource dataSource = new ComboPooledDataSource();for(int i=1;i<=1000000;i++){Connection conn = dataSource.getConnection();if(conn!=null){System.out.println("擷取串連第"+i+"個");conn.close();}}long end = System.currentTimeMillis();System.out.println("用了" + (end-begin)/1000+"秒");}}

5.DBCP的使用:
(1).採用類載入的方式,檔案名稱是:dbcp.properties

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/jdbctestusername=rootpassword=wwh

(2).在代碼中使用(TestDBCP.java)

package cn.wwh.www.java.jdbc.datasource;import java.io.InputStream;import java.sql.Connection;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSourceFactory;/** *類的作用:採用DBCP串連池的方式去擷取資料庫的連結,並測試他的時間 * * *@author 一葉扁舟 *@version 1.0 *@建立時間: 2014-9-1   下午09:27:59 *///測試連接池DBCP的用法public class TestDBCP {public static void main(String[] args) throws Exception {long begin = System.currentTimeMillis();//載入屬性檔案InputStream is = TestDBCP.class.getClassLoader().getResourceAsStream("cn/wwh/www/java/jdbc/config/dbcp.properties");Properties props = new Properties();props.load(is);//建立DBCP串連池工廠BasicDataSourceFactory factory = new BasicDataSourceFactory(); //建立資料來源,即串連池DataSource ds = factory.createDataSource(props);for(int i=1;i<=1000000;i++){//從串連池中取得一個閒置連線物件Connection conn = ds.getConnection();if(conn!=null){System.out.println("擷取串連第"+i+"個");}//將連線物件還回給串連池conn.close();}long end = System.currentTimeMillis();System.out.println("用了" + (end-begin)/1000+"秒");}}

第一的測試時間為18秒,第二個的測試時間為13秒,不同的機器效能不一樣,測試的時間快慢也不可能。但是理論上C3P0的速度應該更快,但是我測試的資料卻恰恰相反,很是奇怪。


總結:

   1>DBCP和C3P0,都是Java開源的,都必須直接或間接實現javax.sql.DataSource介面
   2>DBCP串連池需要dbcp.properties檔案,同時需加入3個對應的jar包
   3>C3P0串連池需要在/WEB-INF/classes/目錄下存放c3p0-config.xml檔案,該類ComboPooledDataSource在建立時
     會自動在指定的目錄下找xml檔案,並載入預設設定

   4>tomcat用的也是c3p0

6.匯入的jar包

commons-dbcp.jar:DBCP實現要匯入的jar

commons-pool.jar: 串連池實現的依賴類

commons-collections.jar :串連池實現的集合類

c3p0-0.9.1.2.jar :C3P0實現要匯入的jar包



mysql 資料庫連接池

把url改成url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=GB2312"
 
怎建立mysql資料庫連接池

直接用其串連函數 mysql_connect
即可連上的。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.