JNDI 路径 Tomcat vs. Jboss [英] JNDI path Tomcat vs. Jboss

查看:28
本文介绍了JNDI 路径 Tomcat vs. Jboss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

      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);
            }

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

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.

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

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

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

谁能解释我为什么要在 Jboss 下的 JNDI 路径中省略comp/env"?

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

推荐答案

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

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 reference to the physical resource defined in the application server, whose JNDI name is proprietary to the application server vendor. This approach enables your code and assembly to be portable to any compliant application server.

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

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

<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>

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

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

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

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

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

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

Alternatively, starting in Java EE 5 (Servlet 2.5), this can be done even easier within your code using the @Resource annotation. 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:

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.

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

Then, you will need to use your application server's proprietary approach for mapping the resource reference to the physical data source that you created on the server, for example, using JBoss's custom deployment descriptors (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>

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

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

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

这篇关于JNDI 路径 Tomcat vs. Jboss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆