Glassfish: Silently ignore unavailable data source at application start up

自作多情 提交于 2019-12-08 02:15:37

问题


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.



来源:https://stackoverflow.com/questions/18445671/glassfish-silently-ignore-unavailable-data-source-at-application-start-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!