Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

where to close DBconnection in struts..urgent..please help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have DAO.java, DAOManager.java for database transactions in my application.
I am using STRuts1.1, eclipse,j2sdk1.4.2, MySql, Tomcat 5.0
where do I close the database connection ..
Is it in DAO class...inside the methods for the transactions.
1.if I do this...I am getting exception saying connection is closed in daomanager.
2.if I do this in DAOmanager class....not in DAO.java..then my application
hangs up after 3 or 4 transactions......

//DAO.java
public void addProgramEnroll(Familyclient familyclient) throws DatastoreException{
PreparedStatement pspprogramenrollinfo = null;
try {
if (conn.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
pspprogramenrollinfo = conn.prepareStatement(SQL_INSERT_PROGRAM);
......
pspprogramenrollinfo.execute();
}
} catch (SQLException ex) {
ex.printStackTrace();
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
} finally {
try {
if (pspprogramenrollinfo != null) {
pspprogramenrollinfo.close();
//conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}


//DAOmanager.java

public void addProgramEnroll(ServletContext sc, Familyclient familyclient)
throws DatastoreException {
Connection conn = getConnection(sc);
FamilyclientDao dao = new FamilyclientDao(conn);
try{
dao.addProgramEnroll(familyclient);
conn.commit();
}catch (Exception ex){
throw DatastoreException.datastoreError(ex);
}finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException sqle) {
log.error("Exception closing connection", sqle);
}
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!