I'm trying to have a simple Jersey based jaxrs listener, running on my existing tomcat 7.0.57. Tomcat has a global config in its context.xml for a jdbc datasource, which I want to use.
My problem is that I can't get the resource to resolve via the @Resource annotation.
Heres a simple test example
@Path("/")
public class TestJersey {
@Resource(name = "jdbc/default")
private DataSource dsA;
@Resource(name = "java:comp/env/jdbc/default")
private DataSource dsB;
@Resource(lookup = "java:comp/env/jdbc/default")
private DataSource dsC;
@Resource(mappedName="jdbc/default")
private DataSource dsD;
@GET
@Produces("text/plain")
public String test() throws NamingException {
StringBuffer ret = new StringBuffer();
ret.append("A: " + dsA + "\n");
ret.append("B: " + dsB + "\n");
ret.append("C: " + dsC + "\n");
ret.append("D: " + dsD + "\n");
DataSource ds1 =
(DataSource) InitialContext.doLookup("java:comp/env/jdbc/default");
ret.append("1: " + ds1 + "\n");
return ret.toString();
}
}
This test app returns the following
A: null
B: null
C: null
D: null
1: org.apache.tomcat.jdbc.pool.DataSource@1518c95{ConnectionPool[d.....
So the jdbc connection is configured, and can be accessed with an explicit doLookup, so why can't I get it working with a @Resource annotation?
My apps web.xml contains
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I've spend some time searching for what I'm doing wrong but I can't find it. I've read posts suggesting adding things like the following web.xml snippets, but they haven't helped
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/default</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
For completeness, my maven dependencies are simply jersey:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.19</version>
</dependency>