release pooled connectoin-Pass by value or reference.

Discussion in 'Java' started by quickcur@yahoo.com, Jul 23, 2005.

  1. Guest

    I think this is a classic java question: Pass by
    reference or by value. I have a DatabaseManager class
    which takes care of getting conection from connection pool
    and releasing the connectoin. I am not sure about the
    release part. Here is my code:

    class MyMainClass{

    public static final void main(String[] args){

    Connection connection =
    DatabaseManager.getConnection();

    ....//jdbc work.

    DatabaseManager.closeConnection(connection);
    ....
    }


    public class DatabaseManager {

    public static Connection getConnection(){
    Connection connection =
    getConectionFromDataSource();

    return connection;
    }

    public static void closeConnection(Connection
    conn){
    if (conn != null){
    try{
    conn.close();
    }catch(SQLException sqle){
    sqle.printStackTrace();
    }
    }
    conn = null;
    }}


    I am not sure if the connection is released....


    Thanks,
     
    , Jul 23, 2005
    #1
    1. Advertising

  2. wrote:
    > I think this is a classic java question: Pass by
    > reference or by value.


    I don't see what this supposed dilemma has to due with the rest of your
    post.

    > I have a DatabaseManager class
    > which takes care of getting conection from connection pool
    > and releasing the connectoin. I am not sure about the
    > release part. Here is my code:
    >
    > class MyMainClass{
    >
    > public static final void main(String[] args){
    >
    > Connection connection =
    > DatabaseManager.getConnection();
    >
    > ....//jdbc work.
    >
    > DatabaseManager.closeConnection(connection);
    > ...
    > }


    The call to DatabaseManager.closeConnection() should be in a finally
    block, unless you like tormenting your DBAs.

    >
    >
    > public class DatabaseManager {
    >
    > public static Connection getConnection(){
    > Connection connection =
    > getConectionFromDataSource();
    >
    > return connection;
    > }
    >
    > public static void closeConnection(Connection
    > conn){
    > if (conn != null){
    > try{
    > conn.close();
    > }catch(SQLException sqle){
    > sqle.printStackTrace();
    > }
    > }
    > conn = null;
    > }}
    >
    >
    > I am not sure if the connection is released....


    Most connection pools work by wrapping the actual Connection
    implementation with their own implementation. The pool's implementation
    of Connection.close() will usually return the connection to the pool.
    Refer to the documentation of your specific connection pool.

    >
    >
    > Thanks,
    >


    HTH,
    Ray
    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Jul 24, 2005
    #2
    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. Darrel
    Replies:
    1
    Views:
    391
  2. Replies:
    5
    Views:
    623
  3. Jerry
    Replies:
    20
    Views:
    7,959
    Roedy Green
    Sep 9, 2005
  4. venkatagmail
    Replies:
    11
    Views:
    692
    James Kanze
    Oct 3, 2007
  5. Tricky
    Replies:
    0
    Views:
    581
    Tricky
    Mar 19, 2009
Loading...

Share This Page