tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From marble4u <marco.blev...@energy4u.org>
Subject Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
Date Wed, 16 Jun 2010 08:27:24 GMT

Hello everybody,


I have been struggeling a long time with this problem, but didn't solve it.
I have a tomcat server 6.0.24 and try to use the resource annotation to get
ms sql database access. It works fine if i do a jndi lookup without
annotations, but if I try to use resource injection my DataSource dataSource
is always null so that I get a NullPointerException when trying to retrieve
a connection through 
dataSource.getConnection()


*Here is my test set up:*


web.xml


&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5"&gt;

  &lt;display-name&gt;ResourceTest&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
    &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
    &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
    &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
    &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
    &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;
    &lt;resource-ref&gt;
	 &lt;description&gt;testDb&lt;/description&gt;
	 &lt;res-ref-name&gt;jdbc/testDb&lt;/res-ref-name&gt;
	 &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;

	 &lt;res-auth&gt;Container&lt;/res-auth&gt;
&lt;/resource-ref&gt;
&lt;/web-app&gt;


context.xml:


&lt;Context path="/ResourceTest"&gt;

&lt;Resource name="jdbc/testDb" auth="Container" type="javax.sql.DataSource"
		maxActive="100" maxIdle="30" maxWait="10000" username="someuser"
password="somepassword"

		driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
		url="jdbc:sqlserver://thinkmarble\vps:1433;databaseName=someName" /&gt;
&lt;/Context&gt;


Here's the code which throws a NullPointerException  in the try clause since
db is null:



package test;
&nbsp;
import java.sql.Connection;
import javax.annotation.Resource;
import javax.sql.DataSource;
&nbsp;
public class TestClass {

&nbsp;
	@Resource(name = "jdbc/testDb") private DataSource db;
&nbsp;
	public void testMethod() {

		try {
			Connection conn = db.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}



Of course I added the jdbc lib to the tomcat lib and tried many things and
as I said: it all works fine with...


...the following jndi lookup code:


package test;

&nbsp;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
&nbsp;
public class JndiTest {

&nbsp;
	public void testMethod() {
		Context initCtx;
		try {
			initCtx = new InitialContext();
			Context envContext = (Context) initCtx.lookup("java:/comp/env");
			DataSource db = (DataSource) envContext.lookup("jdbc/testDb");
			Connection conn = db.getConnection();
		} catch (Exception ex) {

			ex.printStackTrace();
		}
	}
}


Here is the Exception I get:


java.lang.NullPointerException
	at test.TestClass.testMethod(TestClass.java:18)
	at org.apache.jsp.index_jsp._jspService(index_jsp.java:60)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
	at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Unknown Source)



I can remember that it once worked like a charme, but some how it doesn't
anymore - I even downloaded and installed tomcat again and set up a new
workspace in eclipse. Why does it work with JNDI but not with Resource
annotations? I am thankful for any help.


PS: I shortend the code down to the essential lines - please don't argue
about the Exception handling...
-- 
View this message in context: http://old.nabble.com/Resource-Annotation-has-no-effect-but-JNDI-Lookup-works-%28JDBC-Resource%29-tp28900220p28900220.html
Sent from the Tomcat - User mailing list archive at Nabble.com.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message