I am wondering what is best practice when handling database connection in servlet. I am using a connection pool. Do we open one single database connection and use this single connection for all queries (see Case 1) or do we open and close a database connection for each query (Case 2)? Please see code example below:
CASE 1 - Keep database connection alive through whole servlet lifetime:
CASE 2 - Open and close a database connection for each query in servlet:
Then what if we call a method, which is doing two SQL queries without doing other stuff. Should we still close (release) the connection after each query or is faster to keep the connection open as we don't do other stuff between the two queries?:
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: How long time can I keep a database connection open?