Is there a pattern to make sure, that a java ee application deploys, even if the datasource is faulty? My application does not start if the datasource can not be reached. The entity manager can not be initialized and therefor can not be injected in my ejb. This fails the entire deployment.

The application does have a DLQ for when the datasource can not be reached.

Example:

persistance.xml

<persistence-unit name="dataSourceExample" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/dataSourceExample</jta-data-source>        
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.generate_statistics" value="false"/>
    </properties>
</persistence-unit>

datasource config in wildfly

<datasource jndi-name="java:jboss/datasources/dataSourceExample" pool-name="dataSourceExamplePool" use-ccm="false">
    <connection-url>someurl</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <driver>com.oracle.ojdbc</driver>
    <pool>
        <max-pool-size>25</max-pool-size>
    </pool>
    <security>
        <user-name>someusername</user-name>
        <password>somepassword</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
        <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
    </validation>
    <timeout>
        <blocking-timeout-millis>5000</blocking-timeout-millis>
    </timeout>
    <statement>
        <share-prepared-statements>false</share-prepared-statements>
    </statement>
</datasource>

Injected entity manager in ejb

@PersistenceContext(unitName = "dataSourceExample")
private EntityManager dataSourceExampleEm;

I just want the application to be deployed, even if it is not fully functional. It will work later, when the datasource "returns".

1 Answers

3
user3328336 On Best Solutions

Hopefully I understood your question properly.

If the JEE Server bootstraps the EntityManagerFactory and there is no datasource available, but referenced in the persistence.xml then it will fail, which is to be expected because the dependency cannot be resolved.

If the creation of the EntityManagerFactory fails, then the application cannot be startet because its part of the bootstrap process. As far as I know there is no way of forcing a lazy persistence bootstrap or retry if the persistence bootstrap fails. This would be a useful behavior, but this approach came up with microservices architecture and hasn't been implemented by common JEE Servers back in the day.

If you want your application to start without the DataSource available yet, then I think you need to bootstrap the EntityManagerFactory yourself, whereby you can catch errors and try it the next time a EntityManager is tried to be injected. With this approach you will have to manage the EntityManagerFactory and EntityManager lifecycle yourself as well as producers for CDI injection. Injection via @PersistenceUnit will not work then.

Links

Getting a reference to EntityManager in Java EE applications using CDI

https://www.sitepoint.com/cdi-weld-inject-jpa-hibernate-entity-managers/

https://deltaspike.apache.org/documentation/jpa.html

Maybe you provide the error logs you get during bootstrap.