Originally posted by Pat Wallwork:
Hi,
One of the methods is getConnection() which simply returns a Connection object. In that method I use the try/catch/finally block, to try and get a connection, catch any error that may occur, and finally close the connection. The part that puzzles me is I return the Connection object, and then close it in the finally section. Why does this not kill the Connection in the page that called that method? My understanding is that the finally statement executes even when you have used the return statement. Is it possible that my method getConnection() actually passes a copy of the Connection object, and that the finally statement kills the original object? You wouldn't believe how much this has been bugging me trying to understand this! I want to make sure connections are closed so I use finally in the DBUtils to do that, plus the page that calls the method getConnection() also uses con.close() to close with as well...so it does seem like I have 2 connection objects with one method call!
Secondly, in my DBUtils class I have another method that handles pagination for me. Any page I have that makes a database query simply passes the ResultSet to my DBUtils.pagination(ResultSet rs) method and it does the work of setting up the paging. This method has no database connection and just works off the ResultSet...and it works very well! Does the ResultSet maintain some kind of internal connection to the database when it's originally created? When I pass the rs to the pagination() method, is it really a copy of the rs or the actual one?
Originally posted by Pat Wallwork:
This does cause me to ask one other thing though and that is why I would define my own close() method. Normally, what I currently do with any page (servlet, jsp's) that calls my getConnection() method I close the connection, statement, and resultSet in a try/catch block after I am done with it, like this:
Would it then be better for me to define my own close() method in my DBUtils class instead?
As for the ResultSet part, I believe from what your telling me that I don't have to worry about closing it in my pagination(rs) method now as I am only passing the reference of the rs to it, right? The pagination() method returns a String and then that reference to the rs is now out of scope. So in the original page that called the method pagination(rs) the very next line I can do rs.close(); con.close(); ps.close(); and hopefully all database objects are properly closed and my code can carry on.
Destroy anything that stands in your way. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|