5 Replies Latest reply on Dec 11, 2014 5:39 AM by dvarunkumar400

    org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z

    dvarunkumar400

      Hi All,

       

      I am facing below Exception. After getting connection from JNDI mysql-ds.xml and i am trying to validate the connection with below code.In standalone its working fine.

       

      Sample Code:

       

      conn=dataSource.getConnection();

        if(conn==null || !conn.isValid(1)){

             System.out.println("Invalid connection");

             ConnectionPool1.init();

             }else{

             System.out.println("Valid connection");

           }

       

      Exception:

      java.lang.AbstractMethodError: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z

       

      Regards,

      Arun Kumar.D.V.

        • 1. Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
          jaysensharma

          The connection object which you get from the JBoss datasource is actually a WrapperConnection object in your case it is "org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5".

          So before you attempt to  invoke the  isValid(int)  you should cast the  Wrapper connection to the underlying database connection object.    Suppose if you are using Oracle Database then you must cast it to OracleConnection first and then you can invoke the isValid(int)  .

           

          Example:

           

          conn=dataSource.getConnection();
          OracleConnection oracleConnection = (OracleConnection ) conn.getUnderlyingConnection();
            if(oracleConnection==null || !oracleConnection.isValid(1)){
                System.out.println("Invalid connection");
                ConnectionPool1.init();
                }else{
                System.out.println("Valid connection");
              }
          
          

           

           

          Make sure in finally block you close the oracleConnection as well as the WrappedConnection   conn  in order to avoid connection leak possibility.

           

          NOTE:   It is the responsibility of the JDBC driver to provide the implementation of this method.  See [1]

          The driver shall submit a query on the connection or use some other mechanism that positively verifies the connection is still valid when this method is called.

          The query submitted by the driver to validate the connection shall be executed in the context of the current transaction.

           

          [1] http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/sql/Connection.java#Connection.isValid…

          1 of 1 people found this helpful
          • 2. Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
            dvarunkumar400

            Hi Jay Kumar SenSharma,

             

            Thanks a lot for support.

             

            But how can i do the same for mysql connection. As i am using mysql-connector-java-5.1.18-bin jar and conn.getUnderlyingConnection(); method is not available for this driver.

             

            I have alternative method like executing query to check valid connection  by create statement but it take more time so i need better solution.Can you please suggest.

             

            Regards,

            Arun Kumar.D.V.

            • 3. Re: Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
              jaysensharma

              The "getUnderlyingConnection()" method is implemented by  "org.jboss.resource.adapter.jdbc.WrappedConnection" class see [1]

               

              Did you try the following:

               

                  Connection conn=dataSource.getConnection();
                  org.jboss.resource.adapter.jdbc.WrappedConnection wrappedConn = (org.jboss.resource.adapter.jdbc.WrappedConnection) conn;
                  com.mysql.jdbc.MySQLConnection mySQLConnection = (com.mysql.jdbc.MySQLConnection ) wrappedConn.getUnderlyingConnection(); 
              
                  if(mySQLConnection==null || !mySQLConnection.isValid(1)){
                        System.out.println("Invalid connection");
                        ConnectionPool1.init();
                        }else{
                        System.out.println("Valid connection");
                      }
              
              

               

              In JBoss 5 you will find this class "org.jboss.resource.adapter.jdbc.WrappedConnection" present inside the "$JBOSS_HOME/jboss-as/common/lib/jboss-common-jdbc-wrapper.jar" JAR.

               

              [1] http://grepcode.com/file/repository.jboss.org/nexus/content/repositories/releases/org.jboss.jbossas/jboss-as-connector/5…

              • 4. Re: Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
                jaysensharma

                Above is the programming based approach to validate the connection.  However in JBoss you can try using the <validate-on-match> inside your mysql.ds.xml   file which will result in the quick recovery from the database in case of the database outage, It creates some small amount of load on the database because it validates the connection each time the connection is checked out from the pool.   But it will ensure that your application does not get a stale connection.


                See [1]   to know more about the <validate-on-match> and <background-validation>


                [1] ConfigDataSources

                • 5. Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
                  dvarunkumar400

                  HI Jay Kumar SenSharma,

                   

                   

                  Thanks a lot its working fine.

                   

                  Regards,

                  Arun Kumar.D.V.