java JNDI 路径 Tomcat vs. Jboss

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7224216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:11:00  来源:igfitidea点击:

JNDI path Tomcat vs. Jboss

javatomcatjdbcjbossjndi

提问by danny.lesnik

I have DataSource which is configured on Tomcat 6 in context.xml as MyDataSource. And I'm fetching it the following way:

我在 Tomcat 6 上在 context.xml 中配置了 DataSource 作为 MyDataSource。我通过以下方式获取它:

      DataSource dataSource;
            try {
                dataSource = (DataSource) new InitialContext().lookup("java:comp/env/MyDataSource");
            } catch (NamingException e) {
                throw new DaoConfigurationException(
                    "DataSource '" + url + "' is missing in JNDI.", e);
            }

Everything works fine. Now I'm exporting this code to Jboss AP 6. and I configured my dataSource and its connection pool as local-tx dataSource under the same name.

一切正常。现在我将此代码导出到 Jboss AP 6。我将我的数据源及其连接池配置为同名的本地 tx 数据源。

When I'm executing the code above, I'm getting NamingException exception. after some investigation I've found that correct way to call my DataSource under Jboss is

当我执行上面的代码时,我收到 NamingException 异常。经过一番调查,我发现在 Jboss 下调用我的 DataSource 的正确方法是

 dataSource = (DataSource) new InitialContext().lookup("java:/MyDataSource");

Can anybody explain me why should I omit "comp/env" in my JNDI path under Jboss?

谁能解释一下为什么我应该在 Jboss 下的 JNDI 路径中省略“comp/env”?

回答by shelley

The portable approach for defining data sources is to use a resource reference. Resource references enable you to define the JNDI name for your data source, relative to your application naming context (java:comp/env), and then map that logical referenceto the physical resourcedefined in the application server, whose JNDI name is proprietaryto the application server vendor. This approach enables your code and assembly to be portable to any compliant application server.

定义数据源的可移植方法是使用资源引用。资源引用使您可以相对于应用程序命名上下文 ( java:comp/env)定义数据源的 JNDI 名称,然后将该逻辑引用映射到应用程序服务器中定义的物理资源,其 JNDI 名称是应用程序服务器供应商的专有名称。这种方法使您的代码和程序集可以移植到任何兼容的应用程序服务器。

Step 1: Declare and Lookup Resource Reference

步骤 1:声明和查找资源引用

Option 1

选项1

This can be done by declaring a resource-refin your web deployment descriptor (WEB-INF/web.xml):

这可以通过resource-ref在您的 Web 部署描述符 ( WEB-INF/web.xml) 中声明 a 来完成:

<resource-ref>
    <description>My Data Source.</description>
    <res-ref-name>jdbc/MyDataSource</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Within your code, you can then lookup this resource using the JNDI name java:comp/env/jdbc/MyDataSource:

在您的代码中,您可以使用 JNDI 名称查找此资源java:comp/env/jdbc/MyDataSource

dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/MyDataSource");

This JNDI name will not change regardless of the server where the application is deployed.

无论应用程序部署在哪个服务器上,此 JNDI 名称都不会更改。

Option 2

选项 2

Alternatively, starting in Java EE 5 (Servlet 2.5), this can be done even easier within your code using the @Resourceannotation. This eliminates the need for configuring the resource-ref in your web deployment descriptor (web.xml) and prevents the need to perform an explicit JNDI lookup:

或者,从 Java EE 5 (Servlet 2.5) 开始,可以使用@Resource注释在您的代码中更轻松地完成此操作。这消除了在 Web 部署描述符 (web.xml) 中配置资源引用的需要,并防止需要执行显式 JNDI 查找:

public class MyServlet extends HttpServlet {

    @Resource(name = "jdbc/MyDataSource")
    private DataSource dataSource;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // dataSource may be accessed directly here since the container will automatically
        // inject an instance of the data source when the servlet is initialized

}

This approach has the same results as the previous option, but cuts down on the boilerplate code and configuration in your assembly.

此方法具有与前一个选项相同的结果,但减少了程序集中的样板代码和配置。

Step 2: Map Resource Reference to Data Source

步骤 2:将资源引用映射到数据源

Then, you will need to use your application server's proprietary approach for mapping the resource reference to the physical data sourcethat you created on the server, for example, using JBoss's custom deployment descriptors (WEB-INF/jboss-web.xml):

然后,您需要使用应用程序服务器的专有方法将资源引用映射到您在服务器上创建的物理数据源,例如,使用 JBoss 的自定义部署描述符 ( WEB-INF/jboss-web.xml):

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <resource-ref>
        <res-ref-name>jdbc/MyDataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:/MyDataSource</jndi-name>
    </resource-ref>
</jboss-web>

Or, for example, using Tomcat's context.xml:

或者,例如,使用 Tomcat 的context.xml

<Resource name="jdbc/MyDataSource" . . . />

回答by Lukasz Stelmach

You can add to your data source definition the 'jndi-name' tag:

您可以在数据源定义中添加“jndi-name”标签:

jndi-name - the JNDI name under which the DataSource should be bound.

jndi-name - 应绑定数据源的 JNDI 名称。

You can find data source documentation on JBoss wiki: ConfigDataSources

您可以在 JBoss wiki 上找到数据源文档:ConfigDataSources