Thread: Driver.connect Timeout
Is there some way to specify a timeout in the Driver.connect url? MySQL allows one to do the following: "jdbc:mysql://host/database?socketTimeout=5000" We are having some stability problems with the machine running the postgresql server. When some of our automated tests attempt to access the database when the machine is down, they hang trying to open the connection. Thanks Darin
On Mon, 12 Jan 2004, Darin Ohashi wrote: > > Is there some way to specify a timeout in the Driver.connect url? > > MySQL allows one to do the following: > > "jdbc:mysql://host/database?socketTimeout=5000" > > We are having some stability problems with the machine running the postgresql > server. When some of our automated tests attempt to access the database when > the machine is down, they hang trying to open the connection. > This was just recently brought up in this thread: http://archives.postgresql.org/pgsql-jdbc/2004-01/msg00085.php It has some mention of a Connection pool, but that is another problem. I took a quick look at the mysql source and they use reflection to determine if a 1.4 jvm is present and use the timeout in that case or throw an Exception otherwise. For the question of setSoTimeout they only do so once, but not knowing how their protocol works it is uncertain if they don't need to perform multiple reads during connection setup or they are just punting on the problem. Kris Jurka
As it turns out, I have been working on a solution that will allow you to do just what you want. My company has been running into a problem when a database goes down, that the Driver hangs when trying to create a connection. In response, I added in a way to set the socket timeout during connection creation. You can set the timeout on the URL as follows: jdbc:postgresql://host/database?timeout=5000 which would set a timeout of 5000 milliseconds. I've attached the patch files. They apply to the REL_7_4_1 tagged files from the GBorg cvs repository. Jeff On Mon, 12 Jan 2004, Darin Ohashi wrote: > > Is there some way to specify a timeout in the Driver.connect url? > > MySQL allows one to do the following: > > "jdbc:mysql://host/database?socketTimeout=5000" > > We are having some stability problems with the machine running the postgresql > server. When some of our automated tests attempt to access the database when > the machine is down, they hang trying to open the connection. > This was just recently brought up in this thread: http://archives.postgresql.org/pgsql-jdbc/2004-01/msg00085.php It has some mention of a Connection pool, but that is another problem. I took a quick look at the mysql source and they use reflection to determine if a 1.4 jvm is present and use the timeout in that case or throw an Exception otherwise. For the question of setSoTimeout they only do so once, but not knowing how their protocol works it is uncertain if they don't need to perform multiple reads during connection setup or they are just punting on the problem. Kris Jurka
Attachment
On Fri, 30 Jan 2004, Jeff Kolesky wrote: > As it turns out, I have been working on a solution that will allow > you to do just what you want. My company has been running into a > problem when a database goes down, that the Driver hangs when trying > to create a connection. In response, I added in a way to set the > socket timeout during connection creation. Could you be more specific about the problem that this solves? I was under the impression that the call that really needed to be changed was connection = new Socket(host,post) in the PGStream constructor because a dns or other network error could make this take a long time before timing out. Without testing it, your patch looks like it only is useful in the case where a connection has been established, but hasn't gone through the authentication process and the database crashed in that split second. Kris Jurka
Yes, the case we were experiencing was the driver hanging during the native socketRead0 method call after having created the Socket object. We have not been able to reproduce the issue in production, but I have simulated it and tested this code against an open socket that reads data but does not respond. This is certainly not a full implementation of setLoginTimeout()-type functionality. Jeff At 7:53 PM -0500 1/30/04, Kris Jurka wrote: >On Fri, 30 Jan 2004, Jeff Kolesky wrote: > >> As it turns out, I have been working on a solution that will allow >> you to do just what you want. My company has been running into a >> problem when a database goes down, that the Driver hangs when trying >> to create a connection. In response, I added in a way to set the >> socket timeout during connection creation. > >Could you be more specific about the problem that this solves? I was >under the impression that the call that really needed to be changed was >connection = new Socket(host,post) in the PGStream constructor because a >dns or other network error could make this take a long time before timing >out. Without testing it, your patch looks like it only is useful in the >case where a connection has been established, but hasn't gone through the >authentication process and the database crashed in that split second. > >Kris Jurka