Search code examples
javaspringdatasourcejndispring-jdbc

Perform JNDI lookup when database is available in spring


Firstly, let me thank you in advance for your time. Let me introduce my question:

I have an spring application with an "optional" datasource, that performs the jndi lookup in a "lazy way" (lookupOnStartup: false ). Logically, when the datasource is needed, the jndi look is done.

But what happened when the lookup is lazylly done and the database is not available: JndiLookupFailureException is rised. Right.

From that moment, every time you use the datasource, you will get the exception, no matter if the database is up & running in the meantime.

And here is my question: is there any way to detect that the database is up and for the lookup again, in order to get rid of the exception??

Thanks a lot for your time!


Solution

  • Unfortunately this is not possible.

    You could create your very own implementation of a DataSource who implements the Composite-Pattern and switch between the real Database if available or a empty HSQLDB-Database inmemory. Yes, the Connectionpool may filled with HSQLDB-Connections even if the database will be available after lookup but in that case you need to reload the webapp.