release pooled connectoin-Pass by value or reference.

Q

quickcur

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,
 
R

Raymond DeCampo

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.

HTH,
Ray
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top