Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How long should I keep a JDBC connection open?

843859Nov 9 2009 — edited Nov 11 2009
Hi,

My question is regarding database Connection objects.

I have a servlet that accesses a database (mysql) through a plain-old-java-object (thus the servlet does not access the database, but the java object does). This java object, which we will call DataBaseLink, that accesses the database is a singleton object, and I synchronize access to the database.

My question is: how long should I keep a JDBC Connection open. Currently, each time DataBaseLink needs to access the database, I create a new Connection object, open the connection, run the SQL statement, and the close the connection. I do it this way because this is how most the examples I saw do it, but this seems like a lot of waste and I could probably just use one JDBC Connection, keep it alive and then use that same connection to always access the database. Is this correct? Since I synchronize on my access to the database, I should be always allowed to use the same Connection object? Right? Or is the overhead of the creation of a Connection object very small and I should not worry about it?

Any thoughts would be great, thanks

-Adam
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2009
Added on Nov 9 2009
4 comments
1,639 views