Results 1 to 5 of 5
  1. #1
    Pacerier is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default do we need multiple connection objects

    Heys all, i have a class with a static reference to a java.sql.Connection object. i mainly call the object's prepareStatement().

    Every other servlet that needs the DB will basically contact this class and they will all share the same connection object. I'm wondering if i'm going to bump into some real concurrency issues or is it perfectly alright to do so?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I'm pretty sure that this is a bad idea to do. because it's possible to get into a state where the connection is in use by one thread (request from a given user), because each servlet request is invoked in its own thread right. Any failure or jammed state from one request would also effect every user as well.

    The usual way is to have a connection per request (either fetch it inside the servlet as needed, or create a threadLocal object). where the connection is fetched from a DataSource, and when we are finished with the connection object in our servlet, we invoke the connection.close() method, which doesn't actually close the connection, but usually releases it back to the JDBC connection pool, such as the commons-pool project that is bundled with tomcat.

    Additionally, we usually configure the credentials for the data source in the context.xml or the servlet container (e.g. server.xml) as a JNDI data source name, where in our application we declare a reference to this JNDI named object to retreive the DataSource instance, and from there in each servlet where needed invoke DataSource.getConnection().

  3. #3
    Pacerier is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    heys, btw just to check, is it true that if we do not explicitly connection.close then the connection will be closed automatically after x seconds?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Entirely depends on the database settings.
    If you don't explicitly close the connections on a webapp you will almost certainly hit the connection wall and be unable to connect...unless the webapp does very little.

    You should always explicitly close resources you open.

    As already said for your original question, do not share a connection within a multi-threaded environment. They are not thread safe.

  5. #5
    Pacerier is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Instantiating Multiple Graphics objects
    By theCardboardBox in forum New To Java
    Replies: 2
    Last Post: 11-25-2010, 01:26 PM
  2. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 07:37 PM
  3. CMT and Connection Objects
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 10-03-2009, 06:08 PM
  4. how to deserialize multiple objects in a file
    By xcallmejudasx in forum Advanced Java
    Replies: 11
    Last Post: 12-16-2008, 05:29 PM
  5. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •