【问题标题】:Java ComboPooledDataSource exceeds pool size and does not reuseJava ComboPooledDataSource 超出池大小并且不重用
【发布时间】:2019-08-04 19:46:27
【问题描述】:

我有以下代码被其他应用程序调用(我无法更改)以读取数据库。
该方法经常在循环中被调用,并且对 DB 进行 DOS。
在数据库中,我可以看到打开了许多连接......增加到数百个......并且数据库由于负载而崩溃。

// Called in a loop
private <T> T execute(String query, PostProcessor<T> postProc, PreProcessor preProcs) throws OperationFailedException {
    try (Connection conn 
            = Objects.requireNonNull(dataSourceRef.get(), "No Connection").getConnection();
         PreparedStatement smt = conn.prepareStatement(query)) {
        preProc.process(smt);
        return postProc.process(smt.executeQuery());
    } catch (SQLException e) {
        throw new OperationFailedException(e.getMessage(), e);
    }
}

日期源在...之前被初始化

// class variable
// AtomicReference<PooledDataSource> dataSourceRef = new AtomicReference<PooledDataSource>();

// Init method
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass(config.getConnectionDriver());
cpds.setJdbcUrl(config.getConnectionString());
cpds.setUser(config.getConnectionUser());
cpds.setPassword(config.getConnectionPassword());
// cpds.setMaxPoolSize(10);
dataSourceRef.getAndSet(cpds); 

我的问题是为什么会这样。
我认为由于池不是每个查询都应该使用新的连接。 以及通过设置最大池大小这不起作用。

我也尝试使用 try-catch-finally 构造并在使用后关闭 stm 和 conn。 (正如我在某处读到的那样,在高负载情况下最终可能会被称为延迟......我认为可能是这种情况)

但是为什么仍然超出了池大小? 如何限制并阻止该方法,直到连接再次可用,然后再继续?

【问题讨论】:

  • @ScaryWombat:这不是由 try() 语法隐式完成的吗?
  • 保罗,你是对的,我认为只有连接在尝试中 - 我应该在发布之前阅读两遍。

标签: java datasource limit connection-pooling c3p0


【解决方案1】:

在 c3p0 中的连接轮询期间,您必须考虑一些选项。这些在下面给出application.property 文件:

db.driver: oracle.jdbc.driver.OracleDriver // for Oracle
db.username: YOUR_USER_NAME
db.password: YOUR_USER_PASSWORD
db.url: DATABASE_URL
minPoolSize:5 // number of minimum poolSize
maxPoolSize:100 // number of maximum poolSize
maxIdleTime:5 // In seconds. After that time it will realease the unused connection.
maxStatements:1000
maxStatementsPerConnection:100
maxIdleTimeExcessConnections:10000

这里,maxIdleTime 是重点。它定义了这将释放未使用的连接的秒数。排在第二位。

另一个是 minPoolSize 。它定义了在空闲模式下它将保持多少连接。

另一个是 maxPoolSize 。它定义了在加载模式下它将保持的最大连接数。

现在,您如何配置ComboPooledDataSource?代码如下:

@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;
    }

具体实现请查看this thread。这里我添加了实际的实现

编辑(获取连接)

您可以使用以下方式获得连接:

Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> doSomeStuffWith(connection));

如何获得 EntityManager?

@PersistenceContext
private EntityManager entityManager;

希望这会对你有所帮助。

谢谢:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-07-25
    • 2012-07-22
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多