如何在 JAX-RS (Jersey) 应用程序中使用 JNDI 资源?
我正在尝试通过 Tomcat JNDI 资源建立与数据库的连接。今天看了很多文章,似乎没有找到答案。
在我的 server.xml 中,我有:
<GlobalNamingResources>
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="tomcat" password="...."
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3333/tomcat?autoReconnect=true"/>
.....
</GlobalNamingResources>
在我的 Web 服务中,我尝试通过以下方式访问资源:
InitialContext ctx = new InitialContext();
DataSource data = (DataSource)ctx.lookup("java:comp/env/jdbc/MyDB");
Connection conn = data.getConnection();
当我运行代码时,出现此异常:
Nov 2, 2011 1:06:20 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
...
我有最新的 mysql-connector-java-5.1.18-bin .jar
在我的网络应用程序的库和我的 tomcat 库中。
你能帮我让它工作吗?
I'm trying to set up a connection to my database through a Tomcat JNDI resource. I've been looking at many articles today and I can't seem to find an answer.
In my server.xml I have:
<GlobalNamingResources>
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="tomcat" password="...."
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3333/tomcat?autoReconnect=true"/>
.....
</GlobalNamingResources>
In my web service, I attempt to access the resource with:
InitialContext ctx = new InitialContext();
DataSource data = (DataSource)ctx.lookup("java:comp/env/jdbc/MyDB");
Connection conn = data.getConnection();
When I run the code, I get this exception:
Nov 2, 2011 1:06:20 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
...
I have the newest mysql-connector-java-5.1.18-bin.jar
in both my web-app's lib and my tomcat lib.
Can you please help me get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用此代码,仅包含资源名称,并且它有效:
I use this code, with only the name of the resource, and it works:
我们只需要在java:之后提及jndi名称
在您的情况下:
因为您在 server.xml 中配置如下:
We just need to mention the jndi name after java:
In your case:
Because you configured in your server.xml as below:
我的解决方案是切换到数据库信息的属性文件,然后将其与实体管理器一起使用。
My solution was to switch to a properties file for the database information and then utilize it with entity manager.