Posted By:
olivia_kyle
Posted On:
Monday, February 16, 2004 11:25 AM
2 Questions about JDBC: 1) Is a user's database connection closed (or, in my case, returned to the connection pool) when the HttpSession times out or is invalidated (session.invalidate())? 2)In an application with multiple servlets that access the database in different ways, do I need to initialize a connection in each servlet? Like this..... private Connection connection; private DataSource ds; public void init() throws ServletException{ try{ Context ctx=new InitialContext(); ds=(DataSource)ctx.lookup("jdbc/collection"); } catch(NamingException ne){ System.out.println("Error connecting to datasource: " + ne.getMessage());
More>>
2 Questions about JDBC:
1) Is a user's database connection closed (or, in my case, returned to the connection pool) when the HttpSession times out or is invalidated (session.invalidate())?
2)In an application with multiple servlets that access the database in different ways, do I need to initialize a connection in each servlet? Like this.....
private Connection connection;
private DataSource ds;
public void init() throws ServletException{
try{
Context ctx=new InitialContext();
ds=(DataSource)ctx.lookup("jdbc/collection");
}
catch(NamingException ne){
System.out.println("Error connecting to datasource: " + ne.getMessage());
}
}//end init() method
public void destroy(){
try{
connection.close();
}
catch(SQLException sqle){
System.out.println("Error closing the database connection:" + sqle.getMessage());
}
}//end destroy() method
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//other code
connection=ds.getConnection();
//other code
}
<<Less