天天看點

jdbc資料庫連接配接池連接配接

c3p0-config.xml 配置檔案


<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <named-config name="mysql">
        <!-- 配置資料庫使用者名 -->
        <property name="user">root</property>
        <!-- 配置資料庫密碼 -->
        <property name="password">root</property>
        <!-- 配置資料庫連結位址 -->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1?characterEncoding=UTF-8&serverTimezone=Hongkong&useSSL=false&autoReconnect=true&failOverReadOnly=false</property>
       					  <!--   jdbc:mysql://localhost:3306/db1?characterEncoding=UTF-8&serverTimezone=Hongkong&useSSL=false&autoReconnect=true&failOverReadOnly=false-->
        <!-- 配置資料庫驅動 -->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <!-- 資料庫連接配接池一次性向資料庫要多少個連接配接對象 -->
        <property name="acquireIncrement">20</property>
        <!-- 初始化連接配接數 -->
        <property name="initialPoolSize">10</property>
        <!-- 最小連接配接數 -->
        <property name="minPoolSize">5</property>
        <!--連接配接池中保留的最大連接配接數。Default: 15 -->
        <property name="maxPoolSize">30</property>
        <!--JDBC的标準參數,用以控制資料源内加載的PreparedStatements數量。但由于預緩存的statements 屬于單個connection而不是整個連接配接池。是以設定這個參數需要考慮到多方面的因素。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default:0 -->
        <property name="maxStatements">0</property>
        <!--maxStatementsPerConnection定義了連接配接池内單個連接配接所擁有的最大緩存statements數。Default: 0 -->
        <property name="maxStatementsPerConnection">0</property>
        <!--c3p0是異步操作的,緩慢的JDBC操作通過幫助程序完成。擴充這些操作可以有效的提升性能 通過多線程實作多個操作同時被執行。Default:3 -->
        <property name="numHelperThreads">3</property>
        <!--使用者修改系統配置參數執行前最多等待300秒。Default: 300 -->
        <property name="propertyCycle">3</property>
        <!-- 擷取連接配接逾時設定 預設是一直等待機關毫秒 -->
        <property name="checkoutTimeout">1000</property>
        <!--每多少秒檢查所有連接配接池中的空閑連接配接。Default: 0 -->
        <property name="idleConnectionTestPeriod">3</property>
        <!--最大空閑時間,多少秒内未使用則連接配接被丢棄。若為0則永不丢棄。Default: 0 -->
        <property name="maxIdleTime">10</property>
        <!--配置連接配接的生存時間,超過這個時間的連接配接将由連接配接池自動斷開丢棄掉。當然正在使用的連接配接不會馬上斷開,而是等待它close再斷開。配置為0的時候則不會對連接配接的生存時間進行限制。 -->
        <property name="maxIdleTimeExcessConnections">5</property>
        <!--兩次連接配接中間隔時間,機關毫秒。Default: 1000 -->
        <property name="acquireRetryDelay">1000</property>
        <!--c3p0将建一張名為Test的空表,并使用其自帶的查詢語句進行測試。如果定義了這個參數那麼屬性preferredTestQuery将被忽略。你不能在這張Test表上進行任何操作,它将隻供c3p0測試使用。Default: null -->
        <property name="automaticTestTable">Test</property>
        <!-- 擷取connnection時測試是否有效 -->
        <property name="testConnectionOnCheckin">true</property>
    </named-config>
</c3p0-config>
           

 // 建立建構資料源對象DataSource 讀取檔案

    ComboPooledDataSource  dataSource = new ComboPooledDataSource("mysql");

ComboPooledDataSource(); 不添加參數

<default-config> 将配置檔案寫在這裡</default-config>

使用c3p0 必須導包

c3p0-xxxx.jar

mchange-commons-java-xxxx.jar

jdbc.propertiese配置檔案

driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db1?characterEncoding=UTF-8&serverTimezone=Hongkong&useSSL=false&autoReconnect=true&failOverReadOnly=false
name=root
password=root
InitialSize=10
MaxIdle=20
maxActive=50
MinIdle=5
MaxWait=60000
           

導入檔案 設定參數

// 檔案必須在src檔案夾或resources檔案夾下

      InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");

      properties.load(is);

      // 讀取配置檔案屬性

      driverClass = properties.getProperty("driverClass");

      url = properties.getProperty("url");

      name = properties.getProperty("name");

      password = properties.getProperty("password");

      initSize = Integer.valueOf(properties.getProperty("InitialSize"));

      maxSize = Integer.valueOf(properties.getProperty("MaxIdle"));

      // *****連接配接池相關************

      // 設定連接配接資料庫的資訊

      dataSource = new BasicDataSource();

      dataSource.setDriverClassName(driverClass);

      dataSource.setUrl(url);

      dataSource.setUsername(name);

      dataSource.setPassword(password);

      // 設定初始化連接配接數量

      dataSource.setInitialSize(initSize);

      // 設定最大空閑連接配接資料

      dataSource.setMaxIdle(maxSize);

      // 設定最大連接配接數

      dataSource.setMaxTotal(maxActive);

      // 設定最小連接配接數

      dataSource.setMinIdle(minIdle);

      // 設定逾時等待時間以毫秒為機關 6000毫秒/1000等于60秒

      dataSource.setMaxWaitMillis(maxWait);

有道詞典 ComboPooledData ... 詳細 X ComboPooledDataSource資料源= new ComboPooledDataSource(mysql);