Search code examples
javaactivejdbc

How to handle re-connects to the DB with ActiveJDBC


I've a Spring Boot Java application in production that uses ActiveJDBC to access a MariaDB database.

If at launch the application boots before the db server, of if the db server crashes and restarts, the apps doesn't re-estabilish the connection with the db.

ActiveJDBC is on version 1.4.13 and if possible I'd prefer not upgrading it, to avoid possible breakages. The db parameters are configured using the database.properties file and typically the usage pattern is:

try {
    Base.open();
    ...
} finally {
    Base.close();
}

Is there a way to circumvent this problem, without monitoring and relaunching the application? Maybe using connection pools? If this is the case, are there any docs or examples?


Solution

  • If you are using a direct JDBC connection in your database.properties file, a new connection will be open every time you execute Base.open(). This means that any old connection that is broken is not relevant anymore. If you use a JDNI pool such as:

    production.jndi=java:comp/env/jdbc/acme
    

    then you want to configure your containers' pool to ensure that every connection served from the pool is valid just before the pool serves the connection to your app. It is up to the implementation and documentation of your container/pool how to do that.

    In any case, I do not think you are going to have issues.