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.



<persistence-unit name="dataSourceExample" transaction-type="JTA">
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.generate_statistics" value="false"/>

datasource config in wildfly

<datasource jndi-name="java:jboss/datasources/dataSourceExample" pool-name="dataSourceExamplePool" use-ccm="false">
        <valid-connection-checker class-name=""/>
        <stale-connection-checker class-name=""/>
        <exception-sorter class-name=""/>

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

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.


Getting a reference to EntityManager in Java EE applications using CDI

Maybe you provide the error logs you get during bootstrap.