2

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.

Claude
  • 1,724
  • 3
  • 17
  • 46

2 Answers2

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

nickrak
  • 1,635
  • 15
  • 18
  • If I use my own EntityManagerFactory without injection, I have to manage transactions on myself, I suppose. Is that right? – Claude Sep 10 '13 at 10:36
  • Is there a better way such as a configuration in the persistence.xml or a deploy property with glassfish 4? Sometimes our database is down during deployment but I would like to avoid having to manually manage transactions. – bdrx Dec 04 '14 at 17:25
-1

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

James
  • 17,965
  • 11
  • 91
  • 146
  • Yes, of course, but it needs special handling of thrown exceptions depending on the database product. However, I wonder if there is a glassfish or eclipselink property which can be set. Suppose the database is available when the application starts up. If the database becomes unavailable later on this is not a problem for the application until the next business method call. So the behaviour of the application depends on the order of events. – Claude Aug 27 '13 at 13:52
  • In addition to the first comment, what should be done in the ExceptionHandler? Ignore the exception? This would cause EclipseLink to act as this error could be ignored and a bunch of other exceptions will follow. I am looking for a way to start ExclipeLink's verification on first usage not at startup. – Claude Aug 27 '13 at 17:18