zoukankan      html  css  js  c++  java
  • c3p0优化详细配置

    <!--当连接池中的连接耗尽的时候 c3p0 一次同时获取的连接数。Default: 3 --> <propert 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 and bizarre 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>

    <!--当连接池用完时客户端调用 getConnection()后等待获取新连接的时间, 超时 SQLException,如设为 0 则无限期等待。单位毫秒。Default: 0 --> <property name="checkoutTimeout">100</property>

    <!--c3p0 将建一张名为 Test 的空表,并使用其自带的查询语句进行测试。如果 定义了这个参数那么属性 preferredTestQuery 将被忽略。 你不能在这张 Test 表上进行任何操作, 它将 只供 c3p0 测试使用。Default: null--> <property name="automaticTestTable">Test</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> 

    <!--用户修改系统配置参数执行前最多等待 300 秒。Default: 300 --> <property name="propertyCycle">300</property>

    解决 MYSQL 8 小时问题 最近的一个项目在 Hibernate 使用 C3P0 的连接池,数据库为 Mysql。开发测试 没有问题,在运行中每个一段长的空闲时间就出现异常: java 代码 1. org.hibernate.exception.JDBCConnectionException: could not execute query 2. at org.hibernate.exception.SQLStateConverter.convert(SQLStateConve rter.java:74) 3. at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExcepti onHelper.java:43) 4. ....... 5. Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error: 6. ** BEGIN NESTED EXCEPTION ** 7. com.mysql.jdbc.CommunicationsException 8. MESSAGE: Communications link failure due to underlying exception: 9. ** BEGIN NESTED EXCEPTION ** 10.java.net.SocketException 11.MESSAGE: Broken pipe 12.STACKTRACE: 13.java.net.SocketException: Broken pipe 14.at java.net.SocketOutputStream.socketWrite0(Native Method) 15....... 16.** END NESTED EXCEPTION **


    查看了 Mysql 的文档, 以及 Connector/J 的文档以及在线说明发现,出现这种异 常的原因是: Mysql 服务器默认的“wait_timeout”是 8 小时,也就是说一个 connection 空 闲超过 8 个小时, Mysql 将自动断开该 connection。 这就是问题的所在, 在 C3P0 pools 中的 connections 如果空闲超过 8 小时,Mysql 将其断开,而 C3P0 并不知 道该 connection 已经失效,如果这时有 Client 请求 connection,C3P0 将该失 效的 Connection 提供给 Client,将会造成上面的异常。 解决的方法有 3 种: 1. 增加 wait_timeout 的时间。 2. 减少 Connection pools 中 connection 的 lifetime。 3. 测试 Connection pools 中 connection 的有效性。 当然最好的办法是同时综合使用上述 3 种方法, 下面就 DBCP 和 C3P0 分别做一说 明,假设 wait_timeout 为默认的 8 小时 DBCP 增加以下配置信息: 1. //set to 'SELECT 1' 2. validationQuery = "SELECT 1" 3. //set to 'true' 4. testWhileIdle = "true" 5. //some positive integer 6. timeBetweenEvictionRunsMillis = 3600000 7. //set to something smaller than 'wait_timeout' 8. minEvictableIdleTimeMillis = 18000000 9. //if you don't mind a hit for every getConnection(), set to "true" 10.testOnBorrow = "true" C3P0 增加以下配置信息: 1. 2. 3. 4. 5. //获取 connnection 时测试是否有效 testConnectionOnCheckin = true //自动测试的 table 名称 automaticTestTable=C3P0TestTable //set to something much less than wait_timeout, prevents connections from going stale 6. idleConnectionTestPeriod = 18000 7. //set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out 8. maxIdleTime = 25000 9. //if you can take the performance 'hit', set to "true" 10.testConnectionOnCheckout = true

    //初始化时获取三个连接,取值应在 minPoolSize 与 maxPoolSize 之间。Default: 3 initialPoolSize ds.setInitialPoolSize(3);

    //连接池中保留的最大连接数。Default: 15 maxPoolSize ds.setMaxPoolSize(10); 

    // 连接池中保留的最小连接数。 //ds.setMinPoolSize(1);

    //当连接池中的连接耗尽的时候 c3p0 一次同时获取的连接数。Default: 3 acquireIncrement ds.setAcquireIncrement(1);

    //每 60 秒检查所有连接池中的空闲连接。Default: 0 idleConnectionTestPeriod ds.setIdleConnectionTestPeriod(60);

    //最大空闲时间,25000 秒内未使用则连接被丢弃。若为 0 则永不丢弃。Default: 0 maxIdleTime ds.setMaxIdleTime(25000);

    //连接关闭时默认将所有未提交的操作回滚。Default: false autoCommitOnClose ds.setAutoCommitOnClose(true);

    //定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:测试的表必须在初始数据源的时候就存在。Default: null preferredTestQuery ds.setPreferredTestQuery("select sysdate from dual");

    // 因性能消耗大请只在需要的时候使用它。如果设为 true 那么在每个 connection 提交 的时候都将校验其有效性。建议使用 idleConnectionTestPeriod 或 automaticTestTable等方法来提升连接测试的性能。Default: false testConnectionOnCheckout ds.setTestConnectionOnCheckout(true);

    //如果设为 true 那么在取得连接的同时将校验连接的有效性。Default: false testConnectionOnCheckin ds.setTestConnectionOnCheckin(true);

    //定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 acquireRetryAttempts ds.setAcquireRetryAttempts(30);

    //两次连接中间隔时间,单位毫秒。Default: 1000 acquireRetryDelay ds.setAcquireRetryDelay(1000);

    //获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍 有效保留,并在下次调用 getConnection()的时候继续尝试获取连接。如果设为 true,那么 在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false breakAfterAcquireFailure ds.setBreakAfterAcquireFailure(true);

    Java配置c3p0
    import java.sql.Connection;

    import java.sql.SQLException;

    import com.mchange.v2.c3p0.ComboPooledDataSource;

    import com.mchange.v2.c3p0.DataSources;

    public final class ConnectionManager {

       private static ConnectionManager instance;

      private ComboPooledDataSource ds;

      private ConnectionManager() throws Exception {

        ds = new ComboPooledDataSource();

         ds.setDriverClass("oracle.jdbc.driver.OracleDriver");

         ds.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl");

        ds.setUser("test"); ds.setPassword("testtest");}

       public static final ConnectionManager getInstance() {

        if (instance == null) {

          try { instance = new ConnectionManager();} catch (Exception e) { e.printStackTrace(); }

        } return instance;

      }

      public synchronized final Connection getConnection() {

        try { return ds.getConnection(); } catch (SQLException e) { e.printStackTrace();} return null;

      }

       protected void finalize() throws Throwable { DataSources.destroy(ds); //关闭 datasource super.finalize(); }

    } //调用数据库连接池

    import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement;

    public class TestC3p0 {

      public TestC3p0() { // TODO 自动生成构造函数存根 }

      public static void main(String[] args) { // TODO 自动生成方法存根

        ConnectionManager cm = ConnectionManager.getInstance();

        Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select sysdate from dual";

        for (int i = 0; i < 10000; i++) {

          try { conn = cm.getConnection(); System.out.println(conn); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); rs.next();         System.out.println(rs.getString(1)); } catch (Exception ex) { ex.printStackTrace(); } finally {

             if (rs != null) { try { rs.close(); } catch (Exception e) { } }

            if (stmt != null) { try { stmt.close(); } catch (Exception e) { } }

             if (conn != null) { try { conn.close(); } catch (Exception e) { } } }

         try { System.out.println(cm.ds.toString()); Thread.sleep(10000); } catch (Exception e) { // TODO: handle exception } }

      }

    }

    此文为网络转载

  • 相关阅读:
    第五周作业
    第四周作业
    第三周作业(两个题)
    第六周作业
    第五周作业
    第四周作业
    第三周作业
    第二周作业
    求最大值及其下标
    查找整数
  • 原文地址:https://www.cnblogs.com/god-monk/p/5863348.html
Copyright © 2011-2022 走看看