如何使用 Play Framework 访问 JNDI DataSource

How to access JNDI DataSource with Play Framework

根据此文档:

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

我只需要 application.conf 中的另一个条目来在 JNDI 中公开数据源:

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

我已将 "tyrex" % "tyrex" % "1.0.1" 添加到 build.sbt 中的 libraryDepenencies

通过阅读其他几篇关于此的帖子,听起来我应该能够简单地使用

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

从 JNDI 获取数据源。但是,当我尝试这样做时,它会抛出以下异常:

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)

我尝试这样做的主要原因是 Quartz 可以重新使用由 Play 创建的 DataSource/ConnectionPool 而不是在 quartz.properties 中定义另一个。根据文档:

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

我应该在 quartz.properties 中需要以下两行:

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

但是,Quartz 会抛出一堆异常:

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]

我不确定下一步该去哪里。任何帮助将不胜感激。

谢谢。

找到了。

我只需要将 jdbc 添加到 build.sbt 中的 libraryDependencies。这在 JNDI 中创建并公开了数据源。