javax.naming.NameNotFoundException:在此上下文中未绑定名称[jdbc / mydb] [英] javax.naming.NameNotFoundException: Name [jdbc/mydb] is not bound in this Context
问题描述
我知道这个问题已被问过好几次了。我找到了一些链接这里和这里作为我发现的一个例子。但是我尝试了他们的解决方案,我的问题还没有解决......
I know this question has been asked several times. I found some links here and here as an example of what I found. But I try their solutions and my problem is not solved yet...
所以我复制了我所拥有的:
So I copy what I have:
- Eclipse中的J2EE项目。我构建了一个新的.war来安装在应用程序服务器中
- 我在Eclipse中创建了一个
Tomcat 7 Server
并将我的.war添加到它在本地部署它
- A J2EE project in Eclipse. I built a new .war to install in an application server
- I created a
Tomcat 7 Server
in Eclipse and added my .war to it to deploy it locally
所以在我的项目中我有一个 .war
项目和在Eclipse中创建新服务器时自动创建的 Servers
项目。
So phisically in my project I have a .war
project and the Servers
project that auto-creates when you create a new Server in Eclipse.
我试过连接到本地 as400 db
但是当我这样做时,我发现了这个错误:
I tried to connect to a local as400 db
but when I did so, I found this error:
Error com.myprojectpackage.xxx: Name XXX is not bound in this Context
我试过添加 ResourceLink
,如我附上的其中一个链接中所述:
I tried to add a ResourceLink
as said in one of the links I attach:
<ResourceLink name="jdbc/mydb"
global="jdbc/mydb"
type="javax.sql.DataSource" />
首先我尝试在我的应用程序中创建它:
First I tried to create it in my application like this:
-
在我的
web.xml
<资源引用>
< description>我的数据库< / description>的数据源
< res-ref-name> jdbc / mydb< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth> Container< / res-auth>
< / resource-ref>
我创建了一个自定义 context.xml
文件并添加
I created a custom context.xml
file and added
< ResourceLink name =jdbc / mydb
global =jdbc / mydb
type =javax.sql.DataSourcemaxActive =50maxIdle =30maxWait =10000
username =myuser password =mypassauth =Container
driverClassName =com.ibm.as400.access.AS400JDBCDriverurl =jdbc:as400://192.168.1.1; naming = system; errors = full; />
第一次尝试不起作用。所以我去了服务器端,并添加了以下内容:
This first try didn't work. So I went to the server side, and added the following:
- 在
context.xml
文件:
- In
context.xml
file:
< ResourceLink name =jdbc / mydb
global = jdbc / mydb
type =javax.sql.DataSourcemaxActive =50maxIdle =30maxWait =10000
username =myuserpassword =mypassauth =Container
driverClassName =com.ibm.as400.access.AS400JDBCDriverurl =jdbc:as400://192.168.1.1; naming = system; errors = full; />
- 在
server.xml
我在上下文
- In
server.xml
I added the sameResourceLink
insideContext
$中添加了相同的 ResourceLink
b
$ b
第二次尝试也失败了。
This second try failed too.
那么,问题是什么?我该如何解决这个问题?
So, what the problem is? How can I solve this?
推荐答案
您的第一个方法失败了,因为您必须为您的DataSource提供工厂类,以便Tomcat成为知道如何创建连接,使用什么驱动程序等等。
Your first approach failed because you must provide a factory class for your DataSource, so that Tomcat be aware how to create connection, what driver to use and so on.
第二个因为没有正确使用 ResourceLink 元素而失败。
The second one failed because of not properly used ResourceLink element.
如Tomcat文档中所述:
As stated in the Tomcat's documentation:
此元素用于创建链接到全局JNDI资源。
This element is used to create a link to a global JNDI resource.
相反,您应该在 META-INF / context.xml <中创建以下声明/ strong>文件:
Instead, you should create the following declaration into the META-INF/context.xml file:
<Context>
<Resource name="jdbc/mydb"
global="jdbc/mydb"
type="javax.sql.DataSource" maxActive="50" maxIdle="30"
maxWait="10000"
username="myuser" password="mypass" auth="Container"
driverClassName="com.ibm.as400.access.AS400JDBCDriver"
url="jdbc:as400://192.168.1.1;naming=system;errors=full;"/>
</Context>
然后确保META-INF文件夹位于正确的位置 - 您必须将其包含在 .war 包的> / webapp 文件夹。
Then make sure the META-INF folder located at the proper place - you must include it within /webapp folder of your .war package.
现在您可以参考您的数据源通过 JNDI 使用以下名称:
Now you can refer to your DataSource through JNDI by using the following name:
java:comp / env / jdbc / mydb
这篇关于javax.naming.NameNotFoundException:在此上下文中未绑定名称[jdbc / mydb]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!