如何设置< Resource>在Tomcat 7中,这样我就不需要使用"java:/comp/env"在代码中? [英] How to set up a <Resource> in Tomcat 7 so that I don't need to use "java:/comp/env" in the code?

查看:96
本文介绍了如何设置< Resource>在Tomcat 7中,这样我就不需要使用"java:/comp/env"在代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉在Tomcat中设置JNDI资源和设置JNDI资源.

I am new to setting up JNDI resources and setting up JNDI resources in Tomcat.

我继承了servlet应用程序.它通过WebLogic在测试服务器上运行. Servlet应用程序通过以下方式访问其数据库资源:

I inherited a servlet application. It runs on a test server via WebLogic. The servlet application accesses its database resource in the following way:

ctx  = new InitialContext();
ds   = (javax.sql.DataSource)ctx.lookup("myDataBaseName"); 
conn = ds.getConnection();

当我在测试JSP中尝试这样做时,它不起作用.我知道了

When I tried that in a test JSP it doesn't work. I get

javax.naming.NameNotFoundException: Name myDataBaseName is not bound in this Context

但是,如果我这样更改测试JSP代码,则能够使测试JSP工作:

However I was able to make the test JSP work if I altered the test JSP code thus:

ctx  = new InitialContext();
Context envContext = (Context)ctx.lookup("java:/comp/env");
ds   = (DataSource)envContext.lookup("myDataBaseName");
conn = ds.getConnection();

我在TOMCAT_HOME/conf/context.html中有此条目(我只是在盒子上将其用作开发环境)

I have this entry in TOMCAT_HOME/conf/context.html ( I'm just using it as a dev environment on my box )

<Resource name="myDataBaseName"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.OracleDriver"
        url="jdbc:oracle:thin:@blahblahblah:1521:database3"
        username="joeuser"
        password="password"
        maxActive="20"
        maxIdle="30"
        maxWait="-1"/>

</Context>

我在TOMCAT_HOME/conf/web.xml中有这个

And I have this in my TOMCAT_HOME/conf/web.xml:

<resource-ref>
    <res-ref-name>myDataBaseName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

我阅读了大约10个有关相关问题的stackoverflow页面,但是我对设置JNDI资源以抽象出解决我的问题的方法还不够熟悉.

I read about 10 stackoverflow pages on related problems, but I am not familiar enough with setting up JNDI resources to abstract those solutions to my problem.

如何更改设置Tomcat 7的方式,以允许现有的servlet应用程序以"myDatabaseName"的身份访问其数据库?我无法选择更改servlet应用程序中的代码或更改在测试服务器上设置WebLogic的方式.

How can I change the way I have Tomcat 7 set up to allow the existing servlet application to access its database as "myDatabaseName"? I don't have the option of altering the code in the servlet application or altering the way WebLogic is set up on the test server.

我需要更改在我的计算机上(对于开发环境)设置Tomcat 7副本的方式,以允许servlet应用程序以第一行引用代码的样式访问数据库.

推荐答案

我在这里看到2种可能性:

I see 2 possibilities here:

实现一个InitialContextFactory并使其可用于tomcat(通过系统属性),以便new InitialContext()为您提供可以进行查找的上下文.

Implement a InitialContextFactory and make it available to tomcat (via system property), so that new InitialContext() gives you a context the lookup would work on.

将数据源绑定到JNDI中的另一个位置,尽管既不推荐也不对其进行记录,它实际上是有效的(tomcat FM指出该树是只读的,似乎仅适用于子树java:/comp/env):

Bind the data source to another place in the JNDI, which actually works although it is neither recommended nor documented (tomcat FM states the tree being read-only which only seems to apply to the subtree java:/comp/env):

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/myDataBaseName");
ctx.bind("myDataBaseName", ds);

这篇关于如何设置&lt; Resource&gt;在Tomcat 7中,这样我就不需要使用"java:/comp/env"在代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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