Awesome
Awesome

Reputation: 457

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.

Upvotes: 0

Views: 101

Answers (1)

Necreaux
Necreaux

Reputation: 9776

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.

Upvotes: 1

Related Questions