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

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

问题描述

我有DataSource,它在context.xml中的T​​omcat 6上配置为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.我将我的dataSource及其连接池配置为同名的local-tx dataSource。

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部署描述符中声明 resource-ref 来完成( WEB-INF / web.xml ):

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)中配置resource-ref的需要,并且无需执行显式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与Jboss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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