Search code examples
javaplayframeworkquartz-schedulerjndi

How to access JNDI DataSource with Play Framework


According to this documentation:

https://www.playframework.com/documentation/2.5.x/JavaDatabase#exposing-the-datasource-through-jndi

I simply need another entry in my application.conf to expose a DataSource in JNDI:

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.jndiName=DefaultDS

I've added "tyrex" % "tyrex" % "1.0.1" to my libraryDepenencies in build.sbt.

From reading several other posts on this, it sound like I should be able to simply use

DataSource ds = (DataSource) play.api.libs.JNDI.initialContext().lookup("DefaultDS");

To fetch the DataSource from JNDI. However, when I try this it throws the following Exception:

javax.naming.NameNotFoundException: DefaultDS not found
    at tyrex.naming.MemoryContext.internalLookup(Unknown Source)
    at tyrex.naming.MemoryContext.lookup(Unknown Source)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)

The main reason I'm trying to do this is so that Quartz can re-use the DataSource/ConnectionPool created by Play instead of defining another in quartz.properties. According to the docs:

http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigDataSources.html

I should need the following two lines in quartz.properties:

org.quartz.jobStore.dataSource = h2
org.quartz.dataSource.h2.jndiURL = DefaultDS

However, Quartz throws a bunch of exceptions:

java.sql.SQLException: Could not retrieve datasource via JNDI url 'DefaultDS' javax.naming.NameNotFoundException: DefaultDS not found [See nested exception: java.sql.SQLException: Could not retrieve datasource via JNDI url 'DefaultDS' javax.naming.NameNotFoundException: DefaultDS not found]

I'm not sure where to go next. Any help would be appreciated.

Thanks.


Solution

  • Found it.

    I simply needed to add jdbc to my libraryDependencies in build.sbt. This created and exposed the DataSource in JNDI.