This tutorial discusses how to manage database reconnection using in WildFly using validate-on-match and background-validation.

Network outages or Database maintenance may cause WildFly to lose the connection to the database. You can manage database connection validation using the validation element within the datasource section of the configuration file. First choose the validation method, by selecting one of the followings:


When setting in your Datasource:


Then the database connection is validated every time it is checked out from the connection pool using the validation mechanism specified in the next step. If a connection is invalid, a warning is written to the log and it retrieves the next connection in the pool. This continues until a valid connection is found. If you prefer not to cycle through every connection in the pool, you can use the use-fast-fail option. If a valid connection is not found in the pool, a new connection is created. If the connection creation fails, an exception is returned to the requesting application.

Please notice that this setting provides the fastest recovery but creates the highest load on the database, therefore it's not recommended for performance reasons.


When setting in your Datasource:


It means that a background validation will take place every ms as specified by background-validation-millis. The default value for the background-validation-millis parameter is 0 milliseconds, meaning it is disabled by default. This value should not be set to the same value as your idle-timeout-minutes setting.

The lower the value of background-validation-millis, the more frequently the pool is validated and the sooner invalid connections are removed from the pool. However, lower values take more database resources. Higher values result in less frequent connection validation checks and use less database resources, but dead connections are undetected for longer periods of time.

wildfly background validation

Here is a sample datasource which uses background-validation:

  <datasource jta="true" jndi-name="java:jboss/datasources/mydb" pool-name="DemoDB" enabled="true" use-ccm="false">
          <check-valid-connection-sql>select 1</check-valid-connection-sql>