This content has been marked as final.
Show 4 replies
-
1. Re: AS 4.0.5 GA complains about unclosed connections
peterj Jan 17, 2007 2:35 PM (in response to tekniklas)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. -
2. Re: AS 4.0.5 GA complains about unclosed connections
tekniklas Jan 18, 2007 8:12 AM (in response to tekniklas)"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 -
3. Re: AS 4.0.5 GA complains about unclosed connections
peterj Jan 18, 2007 10:41 AM (in response to tekniklas)Sorry, I was typing from memory and not from example code. I meant to also show getting the connection from the datasource and then closing the connection in the fiinally block.
-
4. Re: AS 4.0.5 GA complains about unclosed connections
tekniklas Jan 19, 2007 4:33 AM (in response to tekniklas)"PeterJ" wrote:
Sorry, I was typing from memory and not from example code. I meant to also show getting the connection from the datasource and then closing the connection in the fiinally block.
Thank you Peter. I am happy now that the connection is closing. I am now deploying to the jboss web server which works nicely.
The feature of the application server complaining about unclosed connections is very useful, it helped me find where I had forgotten to close the connection. I hope this feature also is in the jboss web server.
Best regards!