如何使用 c3p0 库设置驱动程序连接属性?

how to set driver connection properties using c3p0 libraries?

我正在从 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" 是 class,它扩展了 BasicDataSource

在 c3p0 中有没有得到相同的结果?

编辑:

明确地说,我能够设置 c3p0 库提供的属性,我正在寻找的是在驱动程序级别设置属性,就像 dbcp2 允许通过 SetConnectionProperties() method.THanks

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

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

@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,每个框架的超时属性都不同,因此在这种情况下,超时由

处理
  • 最大连接年龄
  • 最大空闲时间
  • maxIdleTimeExcessConnections

Managing Pool Size and Connection Age Go To Top

Different applications have different needs with regard to trade-offs between performance, footprint, and reliability. C3P0 offers a wide variety of options for controlling how quickly pools that have grown large under load revert to minPoolSize, and whether "old" Connections in the pool should be proactively replaced to maintain their reliablity.

  • maxConnectionAge
  • maxIdleTime
  • maxIdleTimeExcessConnections

By default, pools will never expire Connections. If you wish Connections to be expired over time in order to maintain "freshness", set maxIdleTime and/or maxConnectionAge. maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool. maxConnectionAge forces the pool to cull any Connections that were acquired from the database more than the set number of seconds in the past.

maxIdleTimeExcessConnections is about minimizing the number of Connections held by c3p0 pools when the pool is not under load. By default, c3p0 pools grow under load, but only shrink if Connections fail a Connection test or are expired away via the parameters described above. Some users want their pools to quickly release unnecessary Connections after a spike in usage that forces a large pool size. You can achieve this by setting maxIdleTimeExcessConnections to a value much shorter than maxIdleTime, forcing Connections beyond your set minimum size to be released if they sit idle for more than a short period of time.

Some general advice about all of these timeout parameters: Slow down! The point of Connection pooling is to bear the cost of acquiring a Connection only once, and then to reuse the Connection many, many times. Most databases support Connections that remain open for hours at a time. There's no need to churn through all your Connections every few seconds or minutes. Setting maxConnectionAge or maxIdleTime to 1800 (30 minutes) is quite aggressive. For most databases, several hours may be more appropriate. You can ensure the reliability of your Connections by testing them, rather than by tossing them. (see Configuring Connection Testing.) The only one of these parameters that should generally be set to a few minutes or less is maxIdleTimeExcessConnections.

我在这个答案中找到了答案: 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);