We have our JBoss and Oracle on separate servers. The connections seem to be dropped and is causing issues with JBoss. How can I have the JBoss reconnect to Oracle if the connection is bad while we figure out why the connections are being dropped in the first place?
6 Answers
Whilst you can use the old "select 1 from dual" trick, the downside with this is that it issues an extra query each and every time you borrow a connection from the pool. For high volumes, this is wasteful.
JBoss provides a special connection validator which should be used for Oracle:
<valid-connection-checker-class-name>
org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker
</valid-connection-checker-class-name>
This makes use of the proprietary ping() method on the Oracle JDBC Connection class, and uses the driver's underlying networking code to determine if the connection is still alive.
However, it's still wasteful to run this each and every time a connection is borrowed, so you may want to use the facility where a background thread checks the connections in the pool, and silently discards the dead ones. This is much more efficient, but means that if the connections do go dead, any attempt to use them before the background thread runs its check will fail.
See the wiki docs for how to configure the background checking (look for background-validation-millis
).

- 398,947
- 96
- 818
- 769
-
11The 'Select 1 from dual' and org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker method are equivalent , although the connection check does provide a level of abstraction. We had to decompile the oracle jdbc drivers for a troubleshooting exercise and Oracle's internal implementation of the ping used with in the connection check, is to perform a 'Select 'x' from dual'. Natch. – abh Jul 01 '15 at 22:01
-
Note that `OracleValidConnectionChecker` had a [minor bug](https://bugzilla.redhat.com/show_bug.cgi?id=699816) in JBoss AS 4 and 5. I also remember that it had performance issues under heavy concurrent load due to using separate pinger thread under the hood. – Vadzim Sep 29 '15 at 12:43
-
3@abh we've had the same issue and found the same thing, `pingDatabase()` will query the database with `SELECT 'x' FROM DUAL;` – Sebastian Apr 03 '18 at 15:57
There is usually a configuration option on the pool to enable a validation query to be executed on borrow. If the validation query executes successfully, the pool will return that connection. If the query does not execute successfully, the pool will create a new connection.
The JBoss Wiki documents the various attributes of the pool.
<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
Seems like it should do the trick.

- 19,408
- 6
- 52
- 50
-
If you already have that line in your cfg, also check that you don't have both "validate-on-match" and "background-validation" set to false (see the linked wiki page for details). – Pino Jan 23 '17 at 09:00
Not enough rep for a comment, so it's in a form of an answer. The 'Select 1 from dual'
and skaffman's org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker
method are equivalent , although the connection check does provide a level of abstraction. We had to decompile the oracle jdbc drivers for a troubleshooting exercise and Oracle's internal implementation of the ping is to perform a 'Select 'x' from dual'
. Natch.

- 422
- 6
- 16
-
not equivalent as using `valid-connection-checker` with correct class provided by vendor is typically optimized for specific database management system – Ewoks Mar 12 '21 at 07:51
JBoss provides 2 ways to Validate connection: - Ping based AND - Query based
You can use as per requirement. This is scheduled by separate thread as per duration defined in datasource configuration file.
<background-validation>true</background-validation> <background-validation-minutes>1</background-validation-minutes>
Some time if you are not having right oracle driver at Jboss, you may get classcast or related error and for that connection may start dropout from connection pool. You can try creating your own ConnectionValidator class by implementing org.jboss.resource.adapter.jdbc.ValidConnectionChecker
interface. This interface provides only single method 'isValidConnection()
' and expecting 'NULL' in return for valid connection.
Ex:
public class OracleValidConnectionChecker implements ValidConnectionChecker, Serializable {
private Method ping;
// The timeout (apparently the timeout is ignored?)
private static Object[] params = new Object[] { new Integer(5000) };
public SQLException isValidConnection(Connection c) {
try {
Integer status = (Integer) ping.invoke(c, params);
if (status.intValue() < 0) {
return new SQLException("pingDatabase failed status=" + status);
}
}
catch (Exception e) {
log.warn("Unexpected error in pingDatabase", e);
}
// OK
return null;
}
}

- 7,721
- 4
- 40
- 55

- 650
- 9
- 9
A little update to @skaffman's answer. In JBoss 7 you have to use "class-name" attribute when setting valid connection checker and also package is different:
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker" />

- 404
- 1
- 7
- 11
We've recently had some floating request handling failures caused by orphaned oracle DBMS_LOCK
session locks that retained indefinitely in client-side connection pool.
So here is a solution that forces session expiry in 30 minutes but doesn't affect application's operation:
<check-valid-connection-sql>select case when 30/60/24 > sysdate-LOGON_TIME then 1 else 1/0 end
from V$SESSION where AUDSID = userenv('SESSIONID')</check-valid-connection-sql>
This may involve some slow down in process of obtaining connections from pool. Make sure to test this under load.

- 24,954
- 11
- 143
- 151
-
-
-
Answer: it is a way to get 30 seconds divided by 60 minutes divided by 24 hours which is the format the time uses. – Nicholas DiPiazza Apr 26 '16 at 21:17
-
@Vadzim This method causes an exception to be thrown when the connection reconnects. any way to make it not throw an exception and restore without failing the request it was on? – Nicholas DiPiazza Apr 27 '16 at 17:01
-
@Nicholas, it seems that [unlike with Tomcat](http://stackoverflow.com/a/32766287/603516) throwing exception is the only way to fail JBoss connection validation. You may suppress it in logs with custom log4j filter. It's harmless otherwise. The connection is validated before being enlisted in handling any real request. – Vadzim Apr 27 '16 at 17:59
-
@Vadzim another side effect, more severe this time, the code that it was executing with a jdbc connection actually fails when the timeout occurs. makes sense. did you institute a retry for this? – Nicholas DiPiazza Apr 27 '16 at 18:24
-
@Nicholas, every fail during connection validation forces it to be kicked out from pool and new fresh connection would be established as needed. App just can't obtain stale (not validated) connection from pool. I guess you're not using the query in the answer in [JBoss datasource config](http://stackoverflow.com/questions/9001166/how-to-create-a-datasource-in-jboss-application-server) but in some other weird way. Then it should be asked as separate question. – Vadzim Apr 27 '16 at 18:46
-
Using it down to the last drop. It must be something else to do with my crazy configuration. When the connection checker sql fails... so does the actual conneciton. – Nicholas DiPiazza Apr 27 '16 at 18:58
-
@Vadzim found out what was the problem. We had a request come in and make *several* calls to the database all referencing the same LOB information. the error is coming from when say the first DB call is made, the second DB call closes the connection, 3rd no longer has the LOB thus the error. It's all in my code – Nicholas DiPiazza Apr 29 '16 at 03:32