my dog learned polymorphism*
The moose likes JDBC and the fly likes DBUtility for closing connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DBUtility for closing connections" Watch "DBUtility for closing connections" New topic
Author

DBUtility for closing connections

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hello,

should the DB utility classes be used for closing the connections, if the connections is also obtained using the same DB utility classe. Since the connection returned from the getConnection() method has still a reference to the connection object which is never closed.

for example in the below mentioned example the connection is not null even after calling the metod for closing rhe connection.


{ please. ignore some typo mistakes}
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

Neeraj Vij wrote:Hello,

.... Since the connection returned from the getConnection() method has still a reference to the connection object which is never closed....
Where does the connection returned from the getConnection() method hold a reference to the connection object?


OCUP UML fundamental and ITIL foundation
youtube channel
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

Neeraj,
A few things are going on here. First, note those helper methods could be static since there is no state stored.

In the close method, dbConnection=null; is setting the parameter passed in to null. The callers value is not null. This is ok, it just doesn't seem to be what you are expecting. Once you call close, the caller has a connection object reference to something that isn't a live connection and will receive errors if trying to call it.

The most important thing is that with this pattern, you have to remember to close the connection everywhere you get one. Some people use a superclass to avoid this problem.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bernhard Haeussermann
Ranch Hand

Joined: Aug 05, 2009
Posts: 38
In the closeConnection() method, the dbConnection variable does indeed receive the value "null". The reason the conn variable in the main() method does not become null, is because in Java parameters are always passed by value. That is, the value of the variable conn (which is the address of the Connection object) is copied into the parameter variable dbConnection. Note that in closeConnection(), because dbConnection now contains the same address as conn, they refer to the same Connection object in the heap. dbConnection then receives the value null. However, because the pass-by-value parameter passing scheme is used, this value is NOT copied back to conn. conn still refers to the Connection object, which still exists in the heap, although it is closed and may therefore not be used anymore. Note that, as long as the Connection is closed, it doesn't matter that it is still in the heap - the space that it occupies will be freed by the Java runtime garbage collector eventually.
I suggest that you read up about parameter passing modes to be able to understand this concept better. For instance, have a look at http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/PARAMS.html.

Finally, whenever you create about any kind of connection, you must ensure that it will always eventually be closed. This should be done by using a finally-block.
Note that in your main() method, if an exception is thrown between lines 43 and 45, the Connection will never be closed.
The correct version of your main() method is:


Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Thanks to all of you for your inputs. Connection object is closed in main() and no operation will be allowed on the same object ,but, since the reference object in main is still not null, will it still be eligible for garbage collection ?

I just wanted some input regarding the performance issue considering the same scenario in a web application, since there could be multiple request which will be opening up connection objects, if those connectio objects are closed but not NULL, will it impact the performance of the application ?

Many Thanks,
Neeraj
Bernhard Haeussermann
Ranch Hand

Joined: Aug 05, 2009
Posts: 38
Hello Neeraj,

Since conn still references the Connection object within your main() method, the Connection object will not be freed up by the garbage collector. However, as soon as the main() method returns, conn goes out of scope, and the Connection object will be eligible for garbage collection. To me this is ok, however, if for some reason you would like the Connection to be eligible for garbage collection while conn is still within scope (i.e. while the main() method is still executing), just set conn equal to null in main().
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DBUtility for closing connections
 
Similar Threads
How to manage the connections in a pool
JDBC Connection Pooling in Websphere
Using datasource for login bean & login page?
Jdbc Connection pooling using apche tomcat6.0.14 and axis2--please help me
connection pool in UDB does not close connections