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:

validate-on-match

When setting in your Datasource:

   <validate-on-match>true</validate-on-match>

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.

background-validation

When setting in your Datasource:

   <background-validation>true</background-validation>
   <background-validation-millis>10000</background-validation-millis>

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">
      <connection-url>jdbc:mysql://localhost:3306/demodb?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <driver>mysql-connector-java-5.1.31-bin.jar</driver>
      <security>
          <user-name>user</user-name>
          <password>password</password>
      </security>
      <validation>
          <check-valid-connection-sql>select 1</check-valid-connection-sql>
          <validate-on-match>false</validate-on-match>
          <background-validation>true</background-validation>
          <background-validation-millis>10000</background-validation-millis>
      </validation>
      <statement>
          <share-prepared-statements>false</share-prepared-statements>
      </statement>
    </datasource>
0
0
0
s2sdefault