jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes How to manage the connections in a pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to manage the connections in a pool " Watch "How to manage the connections in a pool " New topic
Author

How to manage the connections in a pool

Nagaraju Nookala
Greenhorn

Joined: Mar 19, 2004
Posts: 23
Hi Everyone,
I always used the basic syntax for database connections for JDBC calls. To be more specific having the code below:

Connection con = null;
PreparedStatement stmt = null;
try {
con = getConnection();
stmt = con.prepareStatement(sqlQuery);
:
:
}
} catch (SQLException sqle) {

}
finally {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException sqle) {}
}
But, I recently came across a set of classes that are used to have the database access. The code is given below. My question here is, in the class SubClass, I am not closing the Connection but closing the ResultSet and Statement. I am assuming that the BaseClass is taking care of closing the Connections and returning them to the Connection Pool. Is this a right approach? Am I taking care of the exceptions? I am working with the Struts framework. Please advice.
Thanks,
Nagaraju.
public abstract class AbstractBaseClass extends Action {
public AbstractBaseClass() {
super();
}

public abstract Method1 executeFromAction(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
ActionErrors errors,
ActionMessages messages,
Connection conn)
throws Exception;
}

public abstract class BaseClass extends AbstractBaseClass {
public BaseClass() {
super();
}

public Method1 execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws NamingException, SQLException, ClassNotFoundException, Exception {

Connection conn = ConnectionProvider.getConnection();

try {
:
:
}
catch(Exception e)
{
:
:
}
catch(Exception e)
{
:
:
}
finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


}
}
public class SubClass extends BaseClass {
public Method1 executeFromAction(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
ActionErrors errors,
ActionMessages messages,
Connection conn)
throws ParseException, SQLException, UserCausedException,
ClassNotFoundException, NamingException
{

:
:
:

}
public static int getDefaultMethod(Connection c,
int Integer1, String string1, String string2)
throws SQLException
{
String query = "select * from myTable"
PreparedStatement s = c.prepareStatement(query);

try {
ResultSet rs = s.executeQuery();
try {

if (rs.next()) {
doSomething();
}
} finally {
rs.close();
}
} finally {
s.close();
}

}
}
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Nagaraju,
Assuming the base class really is closing the connection it is fine.


[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
Nagaraju Nookala
Greenhorn

Joined: Mar 19, 2004
Posts: 23
Thank you very much, Jeanne.
-Nagaraju
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to manage the connections in a pool
 
Similar Threads
help me, login different categories of users from databse
Exhausted Connection Pool In Websphere
Cannot create iterator for FormBean
Struts:DB Connection with Oracle
Help? Compliation Errors