javapostgresqljpaconnectioneclipselink

Java Service Using JPA and EclipseLink Must Maintain Resilient Connections to PostgreSQL Database


I have an issue where the connection from my Java micro-services drops when the PostgreSQL database service is restarted.

I have implemented this on an external open-source Java service (namely, Keyclaok) which has resolve dthe issue on that service:

<validation>
    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
    <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLStaleConnectionChecker"/>
</validation>

I'm looking to do something similar in my Java Thornttail applications, but I haven't been able to work out how. Perhaps I can edit the persistence.xml somehow:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
             version="3.0">
    <persistence-unit name="pbm" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/***</jta-data-source>

        <properties>
            <property name="eclipselink.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="eclipselink.jdbc.url" value="java:jboss/datasources/greydom"/>
            <property name="eclipselink.target-database"
                      value="org.eclipse.persistence.platform.database.PostgreSQLPlatform"/>
            <property name="eclipselink.connection-pool.max" value="10000"/>
            <property name="eclipselink.connection-pool.wait" value="3000000"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.url" value="java:jboss/datasources/***"/>
        </properties>
    </persistence-unit>
</persistence>

Can anybody help me to update my database connection to ensure it is resilient and maintained?

Extra info


Solution

  • Add the following to custom classes:

    public class JpaExceptionHandler implements ExceptionHandler {
        @Override
        public Object handleException(RuntimeException exception) {
            EclipseLinkException eclipseLinkException = (EclipseLinkException) exception;
            log.error("Custom JpaExceptionHandler EXCEPTION {} ", eclipseLinkException.getUnformattedMessage());
            return null;
        }
    }
    
    public class JpaSessionCustomiser implements SessionCustomizer {
    
        public void customize(Session session) {
            log.debug("Custom JPA Handler:");
            log.debug("Custom JPA Handler: Connected:{}", session.isConnected());
            DatabaseLogin login = (DatabaseLogin)session.getDatasourceLogin();
            login.setConnectionHealthValidatedOnError(false);
            login.setQueryRetryAttemptCount(20);
            login.setPingSQL("select 1 = 1");
        }
    }
    

    Add the following properties to the persistence unit in the persistence.xml:

    <property name="eclipselink.session.customizer" value="com.name.module.util.JpaSessionCustomiser"/>
    <property name="eclipselink.exception-handler" value="com.name.module.util.JpaExceptionHandler"/>