Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)

2010-06-16 Thread marble4u

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






  ResourceTest
  
index.html
index.htm

index.jsp
default.html
default.htm
default.jsp

  

 testDb
 jdbc/testDb
 javax.sql.DataSource

 Container




context.xml:








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



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

 
@Resource(name = "jdbc/testDb") private DataSource db;
 
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;

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

 
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 does

Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)

2010-06-16 Thread marble4u

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



Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)

2010-06-17 Thread marble4u

@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