4 Replies Latest reply on Jan 19, 2007 4:33 AM by Holger Niklas

    AS 4.0.5 GA complains about unclosed connections

    Holger Niklas Newbie

      I get the message "[org.jboss.resource.connectionmanager.CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection"

      What am I missing?

        • 1. Re: AS 4.0.5 GA complains about unclosed connections
          Peter Johnson Master

          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
            Holger Niklas Newbie

             

            "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
              Peter Johnson Master

              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
                Holger Niklas Newbie

                 

                "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!