Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
@gurkan Chris: actually I don't want to use the resource directly in a servlet or JSP - due to architectural reasons - so is there a way to inject resources into plain java classes? @Pid: Thanks for the Information. I will check out the spec and hope that it is not too much to read ;-) And thanks a again for all the hints -- View this message in context: http://old.nabble.com/Resource-Annotation-has-no-effect-but-JNDI-Lookup-works-%28JDBC-Resource%29-tp28900220p28916019.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
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.5gt; lt;display-namegt;ResourceTestlt;/display-namegt; lt;welcome-file-listgt; lt;welcome-filegt;index.htmllt;/welcome-filegt; lt;welcome-filegt;index.htmlt;/welcome-filegt; lt;welcome-filegt;index.jsplt;/welcome-filegt; lt;welcome-filegt;default.htmllt;/welcome-filegt; lt;welcome-filegt;default.htmlt;/welcome-filegt; lt;welcome-filegt;default.jsplt;/welcome-filegt; lt;/welcome-file-listgt; lt;resource-refgt; lt;descriptiongt;testDblt;/descriptiongt; lt;res-ref-namegt;jdbc/testDblt;/res-ref-namegt; lt;res-typegt;javax.sql.DataSourcelt;/res-typegt; lt;res-authgt;Containerlt;/res-authgt; lt;/resource-refgt; lt;/web-appgt; context.xml: lt;Context path=/ResourceTestgt; lt;Resource name=jdbc/testDb auth=Container type=javax.sql.DataSource maxActive=100 maxIdle=30 maxWait=1 username=someuser password=somepassword driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://thinkmarble\vps:1433;databaseName=someName /gt; lt;/Contextgt; 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)
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
Thank you, that helped a lot! I am new to j2ee... I have a lot of application logic that will be used without representation as a jsp or a servlet: objects that extend TimerTask and register at a timer objekt. they execute code which accesses a database (importer classes that collect data from different sources and write it to a database once or twice a day). is that even possible with tomcat/j2ee? do i have to use beans for that which use the resource annotation? i could easily do it with swing etc... but i am asked to deploy it on an apache tomcat server with a web interface (which show the tasks that will be executed). that's why i need the db access. it would be nice if someone could lead me into the right direction - just a rough outline - there is a lot information about standard use cases (displaying webpages, guestbooks, etc...), but i think this is something a little more special. thanks in advance! -- View this message in context: http://old.nabble.com/Resource-Annotation-has-no-effect-but-JNDI-Lookup-works-%28JDBC-Resource%29-tp28900220p28908327.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org