高负载Web应用程序中的Hibernate c3p0 maxPoolSize

时间:2015-12-18 09:07:38

标签: java spring hibernate spring-mvc c3p0

这是关于高负载Web应用程序中c3p0连接池管理器配置的一般问题。 我们在Web应用程序中使用hibernate作为我们的ORM,并且我们使用c3p0作为我们的连接池管理器。我们当前的配置如下所示:

    final com.mchange.v2.c3p0.ComboPooledDataSource dataSource = new com.mchange.v2.c3p0.ComboPooledDataSource();

    PropertyHelper propertyHelper = new PropertyHelper("db.properties");
    String jdbcUrl = propertyHelper.read("jdbcUrl");
    String user = propertyHelper.read("dbuser");
    String password = propertyHelper.read("dbpass");

    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setJdbcUrl(jdbcUrl);
    dataSource.setUser(user);
    dataSource.setPassword(password);
    dataSource.setMaxPoolSize(50);
    dataSource.setMinPoolSize(3);
    dataSource.setAcquireIncrement(1);
    dataSource.setTestConnectionOnCheckin(true);
    dataSource.setMaxIdleTimeExcessConnections(140);
    dataSource.setIdleConnectionTestPeriod(200);
    dataSource.setMaxStatements(0);
    dataSource.setMaxIdleTime(300);

我们在分析时注意到,在加载测试我们的REST API(与数据库连接)时,连接池管理器几乎一直在占用。有500个线程每隔1秒查询不同资源的API,它可以正常工作,平均响应时间为25-50毫秒。但是,只要我们超过它,比如2000个线程查询API,请求开始花费1-3秒,连接池管理器占用了90%的请求时间。

只要我们再次减少请求数量,连接池管理器就可以再次启动,并且每个请求我们都会恢复到25-50毫秒。分析显示没有内存泄漏的迹象。

我的问题是,当你有一个应该能够每秒处理数千个请求的Web应用程序时,配置连接池管理器的正确方法是什么。我无法在该区域找到最佳做法,其他默认/建议max_pool_size在5到100之间进行扩展,具体取决于您正在阅读的位置。

我希望有人能指出我正确的方向。

编辑:

我在@Kayaman推荐的this article中找到了答案。在选择连接池大小时,一个很好的公式是PostgreSQL提供的以下内容:

connections =((core_count * 2)+ effective_spindle_count)

我现在肯定有一些工作要做。

0 个答案:

没有答案