Glassfish: Silently ignore unavailable data source

2019-05-23 18:44发布

问题:

I have an EJB application which uses JPA 2.0 on Glassfish 3.1.2 (provider is EclipseLink). When the database is down the application does not start and even more: can't be deployed. This is because EclipseLink does some initial verification.

Is there a way that the application can be deloyed and started even if the database is down?

Background: The resource being unavailable comes not into play until the first business function is called which accesses the database. From application startup till the first business function call there is a time window where the database may be started.

Changing the defaults in glassfish-resources.xml for the attributes connection-creation-retry-attempts and connection-creation-retry-interval-in-seconds of <jdbc-connection-pool> helps in some way but does still check the database availabilty at startup not at first use.

ExceptionHandler from EclipseLink is not the way I have in mind: when the exception handler comes into play EclipseLink has already started the verification process and hence has tried to connect to the database. The way I am looking for is to postpone the verfification process itself until the first business call.

回答1:

EclipseLink database verification is performed at the first use. However, this first use is most likely a @PersistanceUnit injection point that is handled by the container. @PersistanceUnit injection is handled at deployment time, and there's nothing you can do to catch that. If you don't want EclipseLink to verify at deployment time, you'll need to handle your own EntityManagerFactory.

I'd recommend creating an application context listener, it can hold a copy of your EntityManagerFactory, and be perform tear down when you undeploy/redeploy. During a normal run you can just not set anything up until it is needed. Here's the basic model I would follow: https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom



回答2:

You might be able to use an EclipseLink ExceptionHandler to catch the error.