2 Replies Latest reply on Jun 22, 2016 3:58 AM by Nicklas Karlsson

    Unwrapping datasource connection prevents release?

    Nicklas Karlsson Master

      I have an arquillian test

       

         @Test
         public void testRelease() throws SQLException {
            OracleConnection c = null;
            OracleCallableStatement s = null;
            try {
               c = dataSource.getConnection().unwrap(OracleConnection.class);
               s = (OracleCallableStatement) c.prepareCall("{ ? = pak.func(?) }");
               s.registerOutParameter(1, Types.INTEGER);
               s.registerOutParameter(2, Types.VARCHAR);
               System.out.println(s.execute());
            } catch (SQLException e) {
               e.printStackTrace();
            } finally {
               if (s != null) {
                  s.close();
               }
               if (c!= null) {
                  c.close();
               }
            }
         }
      

       

      But if I have stats on the pool I see that the InUseCount increases for every run.

      If I change to vanilla Connection/CallableStatement, the connections are released.

        • 1. Re: Unwrapping datasource connection prevents release?
          jaikiran pai Master

          Since the pooling logic and keeping track of which connection is in use etc... happens within the wrapped proxy connection that you get from the datasource, I can see why all that logic would be skipped if you use the underlying connection for direct connection management. I would instead suggest changing that code to:

           

          @Test 
            public void testRelease() throws SQLException { 
                OracleConnection c = null; 
                OracleCallableStatement s = null; 
                final Connection wrappedConnection = dataSource.getConnection();
                try { 
                  c = wrappedConnection.unwrap(OracleConnection.class); 
                  s = (OracleCallableStatement) c.prepareCall("{ ? = pak.func(?) }"); 
                  s.registerOutParameter(1, Types.INTEGER); 
                  s.registerOutParameter(2, Types.VARCHAR); 
                  System.out.println(s.execute()); 
                } catch (SQLException e) { 
                  e.printStackTrace(); 
                } finally { 
                  if (s != null) { 
                      try {
                        s.close(); 
                      } catch (Exception ignore)  {// ignore or log}
                  } 
                  // close the original wrapped connection
                  wrappedConnection.close();
                } 
          } 
          
          • 2. Re: Unwrapping datasource connection prevents release?
            Nicklas Karlsson Master

            Heh, I was just about to update the post with the information that unwrapping *after* the getting works ;-)