如何在JAX-RS(Jersey)应用程序中使用JNDI资源?

问题描述 投票:4回答:2

我正在尝试通过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
...

我在网络应用程序的lib和tomcat的lib中都有最新的mysql-connector-java-5.1.18-bin.jar。>

您能帮我解决这个问题吗?

我正在尝试通过Tomcat JNDI资源建立与数据库的连接。我今天看了很多文章,但似乎找不到答案。在我的server.xml中,我有:<...>

mysql java-ee tomcat jndi jax-rs
2个回答
1
投票

我使用此代码,仅使用资源名称,并且可以正常工作:


0
投票

我们只需要在java:

© www.soinside.com 2019 - 2024. All rights reserved.