• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to manage the connections in a pool

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}

}
}
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nagaraju,
Assuming the base class really is closing the connection it is fine.
 
Nagaraju Nookala
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Jeanne.
-Nagaraju
reply
    Bookmark Topic Watch Topic
  • New Topic