【问题标题】:how to set driver connection properties using c3p0 libraries?如何使用 c3p0 库设置驱动程序连接属性?
【发布时间】:2019-09-03 02:22:58
【问题描述】:

我正在从 dbcp2 迁移到 c3p0,

我们通过从 dbcp2 扩展 BasicDataSource 并设置属性来创建数据源。某些属性是通过 setConnectionProperties 方法在驱动程序级别设置的。

在扩展 AbstractComboPooledDataSource 时,我在 c3p0 中没有看到这样的规定。有没有另一种设置方法?

翻阅文档,我发现了一个叫做 connectionCustomizer 的东西,但不确定它是否也一样

这就是我当前使用 dbcp2 设置属性的方式:

this.setConnectionProperties("driver:oracle.jdbc.ReadTimeout=180000");
this.setConnectionProperties("driver:oracle.net.CONNECT_TIMEOUT=180000");

其中“this”是扩展 BasicDataSource 的类

c3p0 中是否有同样的结果?

编辑:

为了清楚起见,我可以设置 c3p0 库提供的属性,我正在寻找的是在驱动程序级别设置属性,dbcp2 允许通过 SetConnectionProperties() 方法进行设置。谢谢

【问题讨论】:

    标签: jdbc c3p0 apache-commons-dbcp


    【解决方案1】:

    您可以从以下答案中获取详细信息,

    https://stackoverflow.com/a/51838455/1529092

    所以基本上你必须做这样的事情,

    @Bean
    public ComboPooledDataSource dataSource(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
    
        try {
            dataSource.setDriverClass(env.getProperty("db.driver"));
            dataSource.setJdbcUrl(env.getProperty("db.url"));
            dataSource.setUser(env.getProperty("db.username"));
            dataSource.setPassword(env.getProperty("db.password"));
            dataSource.setMinPoolSize(Integer.parseInt(env.getProperty("minPoolSize")));
            dataSource.setMaxPoolSize(Integer.parseInt(env.getProperty("maxPoolSize")));
            dataSource.setMaxIdleTime(Integer.parseInt(env.getProperty("maxIdleTime")));
            dataSource.setMaxStatements(Integer.parseInt(env.getProperty("maxStatements")));
            dataSource.setMaxStatementsPerConnection(Integer.parseInt(env.getProperty("maxStatementsPerConnection")));
            dataSource.setMaxIdleTimeExcessConnections(10000);
    
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    

    编辑:

    根据documentation,每个框架的超时属性是不同的,所以在这种情况下,超时由处理,

    • maxConnectionAge
    • 最大空闲时间
    • maxIdleTimeExcessConnections

    管理池大小和连接时间转到顶部

    不同的应用程序在权衡方面有不同的需求 在性能、占地面积和可靠性之间。 C3P0 提供了广泛的 控制池增长速度的多种选择 大负载下恢复为 minPoolSize,以及是否“旧”连接 在池中应主动更换,以保持其 可靠性。

    • maxConnectionAge
    • 最大空闲时间
    • maxIdleTimeExcessConnections

    默认情况下,连接池永远不会过期。如果您希望连接按顺序过期 要保持“新鲜度”,请设置 maxIdleTime 和/或 maxConnectionAge。 maxIdleTime 定义应该允许连接的秒数 在被从池中剔除之前未使用。最大连接年龄 强制池剔除从 数据库过去超过设定的秒数。

    maxIdleTimeExcessConnections 是关于最小化 当池未处于负载状态时,c3p0 池持有的连接数。经过 默认情况下,c3p0 池在负载下会增长,但仅在 Connections 时才会缩小 连接测试失败或通过参数过期 如上所述。一些用户希望他们的池快速释放 使用量激增后不必要的连接 池大小。您可以通过设置来实现这一点 将 maxIdleTimeExcessConnections 设置为比 maxIdleTime 短得多的值, 如果超出您设置的最小大小,则强制连接被释放 他们闲置了超过很短的时间。

    关于所有这些超时参数的一些一般性建议:慢点! 连接池的重点是承担获得一个 只连接一次,然后重用连接很多很多 次。大多数数据库支持保持打开数小时的连接 一次。无需每次都翻遍您的所有连接 几秒钟或几分钟。将 maxConnectionAge 或 maxIdleTime 设置为 1800(30 分钟)相当激进。对于大多数数据库,有几个 小时可能更合适。你可以确保你的可靠性 通过测试它们而不是通过折腾来连接。 (看 配置连接测试。)这些参数中唯一一个 一般应设置为几分钟或更短时间 maxIdleTimeExcessConnections。

    【讨论】:

    • 如何在您创建的数据源中设置“driver:oracle.jdbc.ReadTimeout=180000”?这就是我需要帮助的地方。
    【解决方案2】:

    我在这个答案中找到了答案: c3p0 hangs on getConnection when there is a network failure

    cpds = new ComboPooledDataSource();
    ...
    
    //--------------------------------------------------------------------------------------
    // NOTE: Once you decide to use cpds.setProperties() to set some connection properties,
    //       all properties must be set, including user/password, otherwise an exception
    //       will be thrown
    Properties prop = new Properties();
    prop.setProperty("oracle.net.CONNECT_TIMEOUT",
        Integer.toString(JDBC_CONNECTION_TIMEOUT_IN_MILLISECONDS));
    prop.setProperty("oracle.jdbc.ReadTimeout",
        Integer.toString(JDBC_SOCKET_TIMEOUT_IN_MILLISECONDS));
    prop.setProperty("user", username);
    prop.setProperty("password", password);
    cpds.setProperties(prop);
    

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2017-08-13
      • 2020-10-21
      • 1970-01-01
      相关资源
      最近更新 更多