| 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: 26496
|
|
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
|
 |
 |
|
|
subject: How to manage the connections in a pool
|
|
|