Re: Reestablishing a db connection after a network failure

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 04/29/04


Date: Thu, 29 Apr 2004 08:45:38 -0500


Followups directed to comp.lang.java.programmer.

Pandis Ippokratis wrote:

> I have the following problem: I am implementing a server in Java and
> I use jdbc to connect to an Oracle 9.0.2 db. All seem to work fine,
> but when I intensionally cause a network failure (i.e. I remove the
> network cable) I get the following exception:
>
> java.sql.SQLException: Io exception: Connection reset by peer: socket
> write error
>
> I plug the cable again and I want to reset the java.sql.Connection but
> this seems to not work. Any suggestions?

There may be an option you can use when you set up the connection that
will instruct the driver to attempt to reconnect automatically. The
details and implications should be considered carefully. The MySQL JDBC
driver distributed by MySQL has such a feature, although I'm not sure
whether it handles all cases of connection interruption.

Alternatively, you can wrap the Connection provided by your driver in an
object that handles the details of this scenario, which might include
obtaining a new Connection to replace the interrupted one. If you make
this new object implement Connection itself (and delegate to the
internal collection) then it would be a drop-in replacement. You must
again be careful to consider all the implications, however. For
instance, there are various aspects of DB-side state that are tied to
specific connections; in particular, transactions. There are also
Java-side entities that are tied to specific Connections and would
require considerable additional work to support transparently -- most
particularly Statements and ResultSets.

The best choice may be to better isolate the DB interactions from the
rest of your code so that you can handle exceptions in a sensible way
but in a single place. Implement the Data Access Object pattern, for
instance, and make your data access objects sufficiently smart. If you
need to do so then put a facade around more complicated DB access
activity that knows how to deal with these problems (and others). This
could all be implemented with EJB, which would have some advantages, but
could also be implemented in normal Java code.

John Bollinger
jobollin@indiana.edu



Relevant Pages

  • Re: JDBC Connection Help
    ... database in VBS. ... I have the correct driver 0jdbc.jar which from what I can tell contains ... trying to establish a JDBC connection. ...
    (microsoft.public.scripting.vbscript)
  • Re: Reestablishing a db connection after a network failure
    ... will instruct the driver to attempt to reconnect automatically. ... The MySQL JDBC ... whether it handles all cases of connection interruption. ... Implement the Data Access Object pattern, ...
    (comp.lang.java.programmer)
  • Re: DBC Connection - Protocol error when execute Select
    ... I tested the Program which is shown at the Community wiki:http://community.ingres.com/wiki/Java_Example ... Connection aborted due to communications ... I used JDBC - Driver Version 3.2 with Ingres Release 9.2.0 but this ... JDBC - Version works works only with Version 9.1.0. ...
    (comp.databases.ingres)
  • Re: Jdbc connect to plain text
    ... u can create a DSN or DSN connection with the text file using the driver ... > "JDBC technology is an API that lets you access virtually any tabular ...
    (comp.lang.java.help)
  • Re: Will calling "close()" on a "java.sql.Connection" really close associated resources?
    ... "Releases this Connection object's database and JDBC resources ... Connection pools may not be able to close DB resources automatically. ... Doing so requires proxying a lot of complicated JDBC calls and trying to match the internal state of the driver. ... That's not easy since the definition and lifespan of a resource varies by driver and whether or not the connection is full-duplex. ...
    (comp.lang.java.programmer)