tags:

views:

661

answers:

2

I am trying to connect to DB using the standard JDBC way

connection = DriverManager.getConnection(url, username, password);

Is there a maximum value of timeout on the connection, how long does a connection live, can I increase the value. I want in cases the connection to be open forever , is it a good idea.

+1  A: 

You can set the Timeout on the Drivermanager like this:

 DriverManger.setLoginTimeout(10);
 Connection c = DriverManger.getConnection(url, username, password);

Which would imply that if the connection cannot open within the given time that it times out.

In terms of keeping a connection open forever, its possible if you do not close the connection but it may not be a good idea. Connections should be closed as soon as you are finished with them.

If you want to optimise the opening and closing of connections then you can use a connection pool.

Vincent Ramdhanie
Hmm, IIRC this only sets the timeout how long the DriverManager has to wait before the DB returned a connection. It does not set the connection's timeout. Also see: http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html#setLoginTimeout%28int%29
BalusC
Thats right @BalusC. As I mentioned that timeout is only how long the DriverManager waits for a connection not how long the connection stays open.
Vincent Ramdhanie
I must however admit (and you probably also) that this is the correct answer on the question as literally phrased in topic's subject. But the actual question in the topic message turned out to be something different.
BalusC
+2  A: 

The value is usually DB-controlled. You have no control over it using code. It depends on the DB server used. It is usually around 30 minutes up to one hour.

On the other hand, keeping a Connection open forever is a very bad idea. Best practice is to acquire and close Connection, Statement and ResultSet in the shortest possible scope to avoid resource leaks and potential application crashes caused by the leaks and timeouts.

True, connecting the DB is an expensive task. If your application is supposed to run a relatively long time and to connect the DB fairly often, then consider using a connection pool to improve connecting performance. If your application is a webapplication, then take a look in the appserver's documentation, it usually provides a connection pooling facility in flavor of a DataSource. If it is a client application, then look for 3rd party connection pooling libraries which have proven their robustness with years, such as Apache Commons DBCP (commonly used, used in lot appservers), C3P0 (known from Hibernate) and Proxool (if you want XA connections).

Keep in mind, when using a connection pool, you still have to write proper JDBC code, i.o.w. acquire and close all the resources in the shortest possible scope. The connection pool will on its turn worry about actually closing the connection or just releasing it back to pool for further reuse.

You may get some more insights out of this article how to do the JDBC basics the proper way.

Hope this helps and happy coding.

BalusC
DBCP is a horrible, *horrible* connection pool. Never use it.
erickson
Interesting. I've never had serious problems with it when used in flavor of a Tomcat 6.0 managed datasource. Care to elaborate?
BalusC
Well, you want to use a pool when resources are "expensive" (take some time to create). DBCP (or really the underlying pool) holds a lock on the entire pool while new objects are being produced. This prevents threads that have finished with a resource and are merely trying to return it to block. Meanwhile, other threads are blocked trying to get those resources. Because these lock acquisitions aren't using the concurrent package, they aren't interruptible. This hurts performance under normal conditions. If something is actually wonky with the DB, it gets *really* ugly, really fast.
erickson
Yes, good point. For Tomcat better replace by `org.apache.tomcat.jdbc.pool.DataSourceFactory`. Thanks for heads up, +1.
BalusC