Reestablishing a db connection after a network failure

Discussion in 'Java' started by Pandis Ippokratis, Apr 29, 2004.

  1. Hi,

    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?
     
    Pandis Ippokratis, Apr 29, 2004
    #1
    1. Advertising

  2. 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
     
    John C. Bollinger, Apr 29, 2004
    #2
    1. Advertising

  3. Most connection pool implementations handle this sort of thing, with
    the added benefit of higher performance.

    Apache has a connection pools architecture in their common package:

    http://jakarta.apache.org/commons/dbcp/

    Or, you can roll your own... it's fairly easy to implement the JDBC
    interfaces. Sun had a tutorial on their website at one time
    (java.sun.com), but I can't find anymore.

    //Nathan

    (Pandis Ippokratis) wrote in message news:<>...
    > Hi,
    >
    > 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?
     
    Nathan Zumwalt, May 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Volk
    Replies:
    1
    Views:
    3,180
    Bill Volk
    Jul 2, 2003
  2. Olivier Merigon
    Replies:
    4
    Views:
    6,523
    Roedy Green
    Jul 20, 2004
  3. Ilias Lazaridis
    Replies:
    0
    Views:
    625
    Ilias Lazaridis
    Feb 1, 2005
  4. Replies:
    0
    Views:
    515
  5. --Jani--
    Replies:
    5
    Views:
    207
Loading...

Share This Page