简体   繁体   English

在Play Framework而非Tomcat中使用初始上下文

[英]Using Initial Context with Play Framework instead of Tomcat

So I have a basic DAO class that makes a connection using an InitialContext lookup with Tomcat and Spring and it works perfectly: 因此,我有一个基本的DAO类,该类使用与Tomcat和Spring的InitialContext查找进行连接,并且运行良好:

Context initCtx = new InitialContext();                        
initialize((DataSource) initCtx.lookup(resourceName));         

Now, I have extended that basic DAO and am using the child class in a play framework application. 现在,我扩展了该基本DAO,并在play框架应用程序中使用child类。 The initial context lookup, however, does not work on the play application. 但是,初始上下文查找不适用于播放应用程序。 The error says: 错误提示:

Caused by: javax.naming.NotContextException: comp is not a subcontext
    at tyrex.naming.EnvContext.internalLookup(Unknown Source) ~[tyrex-1.0.1.jar:1.0.1  November 11 2003 1703]
    at tyrex.naming.EnvContext.lookup(Unknown Source) ~[tyrex-1.0.1.jar:1.0.1  November 11 2003 1703]
    at tyrex.naming.java.JavaContext.lookup(Unknown Source) ~[tyrex-1.0.1.jar:1.0.1  November 11 2003 1703]
    at javax.naming.InitialContext.lookup(InitialContext.java:417) ~[na:1.8.0]
    at DatabaseFactory.<init>(DatabaseFactory.java:63) ~[Utilities-SNAPSHOT.jar:na]

Is there anyway to use the initial context lookup function while still using the play framework? 无论如何,仍然在使用play框架的同时使用初始上下文查找功能吗?

Play's JNDI environment is provided by an in-memory JNDI service provider . Play的JNDI环境由内存中的JNDI服务提供商提供 There is a Play helper class you can use to get hold of the InitialContext called play.api.libs.JNDI . 还有就是你可以用它来获得的保持状态的游戏辅助类InitialContext称为play.api.libs.JNDI You can use this to lookup your datasource. 您可以使用它来查找数据源。

Using this benchmark test app as an example and adding the following action: 该基准测试应用程序为例,并添加以下操作:

public Application extends Controller {

    ... 

    public static Result getDataSource() throws NamingException {
        DataSource dataSource = (DataSource) play.api.libs.JNDI.initialContext().lookup("DefaultDS");

        return ok(dataSource.toString());
    }
}

would lookup the datasource bound to the name DefaultDS in the application.conf 将在application.conf查找绑定到名称DefaultDS的数据源。

Looking at your exception, it seems like you're looking up the InitialContext and then resources with java:comp/.../resourceName in your utility library. 查看您的异常,似乎您正在查找InitialContext ,然后在实用程序库中查找带有java:comp/.../resourceName This won't play (excuse the pun) well in your Play app out of the box. 开箱即用的Play应用程序无法很好地播放(打扰)。 I would suggest you lookup your DataSource in a Play specific way, as per the example above. 我建议您按照上面的示例以Play特定的方式查找DataSource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM