• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

DB Connection LEaking

Guys,

One quick question. Is my code below correct? This is about Database Connection Leaking.

Connection conn = null;

public void methodA() {
    conn = [get DB Conn here]; // it works okay
 
    methodB(conn);

    cleanUp();  // clean up will clear up the DB Connection
}

public void methodB(Connection conn) {
    // do I need to clear up the connection in methodB?
}

Question: do I need to clear up the connection in methodB? I do not think so. Am I right?

David
0
suprapto45
Asked:
suprapto45
3 Solutions
 
suprapto45Author Commented:
Sorry,

methodA and methodB are located in the different class.

David
0
 
mnrzCommented:
no need to do that
because the reference of the objects will sent through the methods so if you clear it up in method B then in method A calling cleanUp() is useless
0
 
mnrzCommented:
however I don't know what exactly do you mean by cleaning up the connection.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
suprapto45Author Commented:
Cleaning up the connection means

if (conn != null) {
    conn.close();
    conn = null;
}

So, if I do the clean up in methodA, I do not need to clean it up in methodB. Am I right? What if they are in two different classes?

David
0
 
mnrzCommented:
yes you right
even if they are in different classes, as I said the only the reference of the Objects will be transfered through calling methods because in the heap, there is only one object but with one or more references to that object.

conn2 -----> [Connection Object in HEAP]
                           ^
                            |
conn1 --------------

if you change any attribute by reference conn1, the reference conn2 will change as well
0
 
suprapto45Author Commented:
Okay,

Thanks. I will leave it open for another hour or so to see if anyone else would like to share some idea or not.

Thanks again.
David
0
 
mnrzCommented:
please note this is only for objects not primitive types like int, long etc.
0
 
Ajay-SinghCommented:
You logic should be like this:


public void methodA() {
    conn = [get DB Conn here]; // it works okay

    try {
       methodB(conn);
    } finally {
       cleanUp();  // clean up will clear up the DB Connection
    }
}


If you are not cleaning up in finally - the connection will not get closed when an exception occurs.
0
 
ksivananthCommented:
>>if (conn != null) {
    conn.close();
    conn = null;
}
>>

if you do this, there is no re-use of connection. Rather use a connection pool and get the conn from it and return it back to it when done with that.

>>So, if I do the clean up in methodA, I do not need to clean it up in methodB.

Yes.

>>What if they are in two different classes?

No matter!
0
 
suprapto45Author Commented:
Thanks to all of you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now