Maintain DB connection

Discussion in 'Java' started by Ken, Oct 6, 2009.

  1. Ken

    Ken Guest

    Right now for testing I am opening up a DB connection and closing it
    for every query. This is because I was just doing quick testing.
    What I am going to do is make the connection a singleton, open the
    connection the first query (or perhaps even before) and then only
    reopen the connection if the DB has closed it (an exception is thrown
    by the SQL statement), I'll put a Boolean condition in to test if I've
    tried to recover once and if it fails twice bail.

    I also think this is such a simple thing there might be some well
    known object to do this for me and handle further db issues that I
    would end up hand rolling too?

    Any advice?
    Ken, Oct 6, 2009
    #1
    1. Advertising

  2. Ken

    Arne Vajhøj Guest

    Ken wrote:
    > Right now for testing I am opening up a DB connection and closing it
    > for every query. This is because I was just doing quick testing.
    > What I am going to do is make the connection a singleton, open the
    > connection the first query (or perhaps even before) and then only
    > reopen the connection if the DB has closed it (an exception is thrown
    > by the SQL statement), I'll put a Boolean condition in to test if I've
    > tried to recover once and if it fails twice bail.
    >
    > I also think this is such a simple thing there might be some well
    > known object to do this for me and handle further db issues that I
    > would end up hand rolling too?
    >
    > Any advice?


    Drop the described approach, use a connection pool and use a
    pattern of:
    open connection
    use connection
    close connection

    Arne
    Arne Vajhøj, Oct 6, 2009
    #2
    1. Advertising

  3. Ken

    Lew Guest

    Ken wrote:
    >> Right now for testing I am opening up a DB connection and closing it
    >> for every query. This is because I was just doing quick testing.
    >> What I am going to do is make the connection a singleton, open the


    Singletons are often very bad.

    Are the connection objects thread-safe? How do you know?

    >> connection the first query (or perhaps even before) and then only
    >> reopen the connection if the DB has closed it (an exception is thrown
    >> by the SQL statement), I'll put a Boolean [sic] condition in to test if I've
    >> tried to recover once and if it fails twice bail.
    >>
    >> I also think this is such a simple thing there might be some well
    >> known object to do this for me and handle further db issues that I
    >> would end up hand rolling too?
    >>
    >> Any advice?


    Arne Vajhøj wrote:
    > Drop the described approach, use a connection pool and use a
    > pattern of:
    > open connection
    > use connection
    > close connection


    +1

    You will not outperform a pooling driver with a hand-wheel-reinventing-coded
    version.

    The Apache project offers DBCP (DataBase Connection Pool)
    <http://commons.apache.org/dbcp/>
    (which in turn requires <http://commons.apache.org/pool/>)
    as one possible solution. Often there are RDBMS-specific solutions as well,
    such as the javax.sql.ConnectionPoolDataSource JDBC driver for Postgres.
    <http://jdbc.postgresql.org/documentation/84/datasource.html>
    though they suggest that it's better to the application server's connection
    pooling if available.

    --
    Lew
    Lew, Oct 6, 2009
    #3
  4. Ken

    Wojtek Guest

    Ken wrote :
    > I also think this is such a simple thing there might be some well
    > known object to do this for me and handle further db issues that I
    > would end up hand rolling too?


    Google proxool

    --
    Wojtek :)
    Wojtek, Oct 6, 2009
    #4
  5. Ken

    Roedy Green Guest

    On Mon, 5 Oct 2009 17:04:24 -0700 (PDT), Ken <>
    wrote, quoted or indirectly quoted someone who said :

    >I also think this is such a simple thing there might be some well
    >known object to do this for me and handle further db issues that I
    >would end up hand rolling too?


    see http://mindprod.com/jgloss/jdbcconnectionpooling.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Smart data structures and dumb code works a lot better than the other way around.
    ~ Eric S. Raymond The Cathedral and the Bazaar
    Roedy Green, Oct 6, 2009
    #5
  6. In article <>, Wojtek <>
    wrote:

    > Ken wrote :
    > > I also think this is such a simple thing there might be some well
    > > known object to do this for me and handle further db issues that I
    > > would end up hand rolling too?

    >
    > Google proxool


    Also have a look at this lightweight standalone JDBC connection pool
    manager. I use it with H2 Database, but it should work with any
    ConnectionPoolDataSource:

    <http://www.source-code.biz/snippets/java/8.htm>
    <http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Oct 7, 2009
    #6
    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. twahl
    Replies:
    2
    Views:
    618
    Tian Min Huang
    Jun 30, 2003
  2. Vikram Sinha
    Replies:
    0
    Views:
    426
    Vikram Sinha
    Jul 17, 2003
  3. Satish Appasani

    Maintain an uploaded Image in a Session

    Satish Appasani, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    Satish Appasani
    Jul 18, 2003
  4. Replies:
    0
    Views:
    563
  5. Fenster Blick
    Replies:
    2
    Views:
    101
    Fenster Blick
    Dec 15, 2007
Loading...

Share This Page