[英]JDBC connection pooling library which can handle connections that go idle often nicely?
Let's say there is this code which lives in a server side backend: 假设这个代码存在于服务器端后端:
class Foo {
void doSomething() {
try (Connection c = dataSource.getConnection()) {
executeSQL(c);
externalApiCall(); // this can be slow
executeSQL(c);
}
}
}
The problem here is that externalApiCall()
can be very slow, and it keeps a lot of database connections open (even though it's not used) and it can lead to running out of the maximum number of the connections. 这里的问题是externalApiCall()
可能非常慢,并且它保持大量数据库连接打开(即使它没有被使用),并且它可能导致用尽最大数量的连接。 It will be harmful to other parts of my app which don't depend on any external API, and I want to avoid this. 它对我的应用程序的其他部分有害,不依赖于任何外部API,我想避免这种情况。
The code above is simplified and in reality I use something like a ServletFilter which puts Connection into ThreadLocal when it receives a http request, so it's difficult to change the overall mechanism which opens a Connection at the beginning of business logic. 上面的代码是简化的,实际上我使用类似ServletFilter的东西,它在收到http请求时将Connection放入ThreadLocal,因此很难改变在业务逻辑开始时打开Connection的整体机制。
So my question is: isn't there any nice connection pooling library, a wrapper or something that can handle such a situation nicely? 所以我的问题是:是不是有任何不错的连接池库,包装器或能够很好地处理这种情况的东西? For example, something which opens a real database connection only when a Statement object is created and close the connection afterwards automatically when the Statement is closed. 例如,只有在创建Statement对象时才会打开真正的数据库连接,并在Statement关闭后自动关闭连接。 Currently I use Tomcat DBCP. 目前我使用的是Tomcat DBCP。
Use cp3 connection pooling. 使用cp3连接池。
The property max_idle_time may satisfy your requirements here. max_idle_time属性可能满足您的要求。
Default: 0 默认值:0
Seconds a Connection can remain pooled but unused before being discarded. 秒可以在丢弃之前保持连接但未使用。 Zero means idle connections never expire. 零意味着空闲连接永不过期。
For more info, refer https://www.mchange.com/projects/c3p0/ 有关详细信息,请参阅https://www.mchange.com/projects/c3p0/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.