-
1. Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
jaysensharma Dec 11, 2014 3:52 AM (in response to dvarunkumar400)1 of 1 people found this helpfulThe 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.
-
2. Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
dvarunkumar400 Dec 11, 2014 4:06 AM (in response to jaysensharma)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 Dec 11, 2014 4:38 AM (in response to dvarunkumar400)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.
-
4. Re: Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
jaysensharma Dec 11, 2014 4:52 AM (in response to dvarunkumar400)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>
-
5. Re: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5.isValid(I)Z
dvarunkumar400 Dec 11, 2014 5:39 AM (in response to jaysensharma)HI Jay Kumar SenSharma,
Thanks a lot its working fine.
Regards,
Arun Kumar.D.V.