开发者

Connection Pooling vs Per-Thread JDBC Connection

开发者 https://www.devze.com 2022-12-31 04:46 出处:网络
Which of these approaches is better: connecti开发者_运维问答on pooling or per-thread JDBC connections?Connection Pooling for sure and almost always.

Which of these approaches is better: connecti开发者_运维问答on pooling or per-thread JDBC connections?


Connection Pooling for sure and almost always.

Creating new database connection is very costly for performance. And different DB engines (depending on licensing or just settings) has different maximum number of connections (sometimes it even 1, usually not more then 50).

The only reason to use Per-Thread connections is if you know that there are certain small number of persistent threads (10 for example). I can't imagine this situation in real world.


Definitely connection pooling. Absolutely no reason to create a new connection for each thread. But, it might make sense to use the same connection for an entire HTTP request for example (especially if you need transactions).

You can easily configure the connection pooling framework to have a min and max number of connections depending on the database that you are using. But before going too high with the max number of connections, try using caching if you have performance issues.


For web apps, connection pooling is generally the right answer for reasons other have already offered.

For most desktop apps running against a database, a connection pool is no good since you need only one connection and having multiple connections consumes resources on the DB server. (Multiply that by the number of users.) Here the choice is between a single persistent connection or else just create the connection on demand. The first leads to faster queries since you don't have the overhead of building up and tearing down the connection. The second is slower but also less demanding on the DB server.

0

精彩评论

暂无评论...
验证码 换一张
取 消