Automatically reconnect to a database after a failure in JBoss EAP 7

My JBoss EAP 7 server couldn’t cope with a failing database. After a database restart or failure, e.g. due to maintenance, it simply would not connect to the database again automatically. The application simply stopped working as soon as the database was unavailable for a short period of time.

JBoss’s server.log was full of (not very helpful) error messages like this:

2017-03-01 12:05:18,175 ERROR [it.macke.repository.UserRepository] (default task-17) Error reading user: org.hibernate.exception.JDBCConnectionException: could not prepare statement: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: could not prepare statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(
Caused by: org.hibernate.exception.JDBCConnectionException: could not prepare statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(
Caused by: java.sql.SQLNonTransientConnectionException: Connection is close
    at org.mariadb.jdbc.internal.util.ExceptionMapper.get(

To make the application work again, I had to restart JBoss and every now and then even the whole Windows server on which it runs.

As it turns out, this is a common problem with JBoss. However, the solution is quite easy. You only need to configure JBoss to validate the database connections. Here’s how this would look in standalone.xml (take a look at the content of element validation):

<datasource jndi-name="java:jboss/jdbc/MyDS" pool-name="MyDS">
        <check-valid-connection-sql>select 1</check-valid-connection-sql>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>

And if you want to script the setting, here’s the code for the CLI:

/subsystem=datasources/data-source=MyDS:write-attribute(name=check-valid-connection-sql,value="select 1")

The above settings make JBoss validate the connection on every new request (validate-on-match). But you can also have it do the checking in the background, e.g. every few seconds. And you need to consider the correct setting for your database. I use MariaDB/MySQL for my application and the exception-sorter and check-valid-connection-sql might be different depending on your database (e.g. and select 1 from dual for Oracle). Take a look at the available parameters here: Database Connection Validation JBoss EAP 7 – Configuration Guide

How to create and deploy a web service with Java EE 7 and Wildfly 9

Apparently, creating and deploying an web service with Java EE 7 is just as easy as it was with Java EE 6. This maybe due to the fact, that support for web services and SOAP in JAX-WS hasn’t changed much since Java EE 6, as Adam Bien points out here: The State of SOAP / JAX-WS. However, just for the record, here’s the basic code needed to provide a simple web service:

package [...];

import javax.jws.WebMethod;
import javax.jws.WebService;

public class Hello
    private final String message = new String("Hello, ");

    public Hello()

    public String sayHello(final String name)
        return message + name + ".";

Simply compile and deploy the WAR file (e.g. YourApplicationName.war), e.g. into \standalone\deployments of the standalone Wildfly installation. You should now be able to access the generated WSDL under http://localhost:8080/YourApplicationName/Hello?wsdl.