简体   繁体   中英

Where to close Connection using Connection Pooling in HTTP Servlets

Which one is better approach?

a)Get connection from Connection Pool at the beginning of doXXX() method and close at the end.

Use this connection throughout doXXX() method. This way it will fetch connection only once from the connection pool but Connection will be open for the whole time it takes to execute doXXX().

b)Get connection from Connection Pool for each db operation and close.

It will fetch connection from the connection pool for every db operation but will be closed immediately.

It depends. Getting a connection from the pool, worst case could result in the expensive creation of a new connection which might be slow. Between DB calls, how long is it going to be doing stuff? In general, releasing after each operation will slow things down for the specific process, but speed things up for other processes, and vice versa. You only really have to worry about this under massive load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM