[jboss-user] [Installation, Configuration & Deployment] - Re: AS 4.0.5 GA complains about unclosed connections

tekniklas do-not-reply at jboss.com
Thu Jan 18 08:12:07 EST 2007


"PeterJ" wrote : You code should look like:
  | 
  | DataSource ds = null;
  |   | try {
  |   |  ds = ctx.lookup(...);
  |   |  --do database stuff --
  |   | } finally {
  |   |  if (ds != null) ds.close();
  |   | }
  | 
  | If you don't close the datasource before exiting the method, you get that warning.

Thank you very much for your reply. Indeed I had forgotton to close a Connection object. But I cannot find a close() method for javax.sql.Datasource, is there one?
My code looks like this:

    
  |       
  |     
  |     public static Connection getConnection() {
  |         try {
  |             Context ctx = new javax.naming.InitialContext();
  |             javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:MySqlDS");
  |             try {
  |                 return ds.getConnection("niklas", "password");
  |             }catch(SQLException se){
  |                 log.error("SQLException in DB.getConnection: ", se);
  |                 return getDMConnection();
  |                 
  |             }
  |         }catch(NamingException ne){
  |             log.error("NamingException in DB.getConnection: ", ne);
  |         }
  |         return getDMConnection();
  |     }
  |     public static Connection getDMConnection() {
  |         try {
  |             Class.forName("com.mysql.jdbc.Driver").newInstance();
  |             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?user=niklas&password=password");
  |             return conn;
  |         }catch(Exception ne){
  |             log.error("Exception in DB.getConnection: ", ne);
  |         }
  |         return null;
  |     }
  | 

It seems to be working now.

Thanks

Niklas

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4003291#4003291

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4003291



More information about the jboss-user mailing list