You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by marble4u <ma...@energy4u.org> on 2010/06/16 10:27:24 UTC
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
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>ResourceTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>testDb</description>
<res-ref-name>jdbc/testDb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
context.xml:
<Context path="/ResourceTest">
<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" />
</Context>
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 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.
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
Posted by Gurkan Erdogdu <gu...@yahoo.com>.
Then you have to use
InitialContext # lookup("java:/comp/env/_Resource_Id_In_your_web_Xml").
As we said, if you want to inject dependencies to your classes, those classes must be known by the container.
--Gurkan
________________________________
From: marble4u <ma...@energy4u.org>
To: users@tomcat.apache.org
Sent: Thu, June 17, 2010 6:37:56 PM
Subject: 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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC
Resource)
Posted by marble4u <ma...@energy4u.org>.
@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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
Posted by Gurkan Erdogdu <gu...@yahoo.com>.
Chris;
This example is not mean that put your database architecture on top of this :) Just shows that container injects the resource if you use container aware class.
Otherwise stick with JPA :)
Thanks;
--Gurkan
________________________________
From: Christopher Schultz <ch...@christopherschultz.net>
To: Tomcat Users List <us...@tomcat.apache.org>
Sent: Thu, June 17, 2010 2:37:05 AM
Subject: Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Gurkan,
On 6/16/2010 10:44 AM, Gurkan Erdogdu wrote:
> Please define some servlet and try with it,
>
> class TestClass extends HttpServlet{
>
> private @Resource(name="jdbc/TestDb") DataSource datasource;
>
> @PostConstruct
> public void postConstruct(){
> //Check datasource here
> }
> }
While this example appears to work, I might recommend to the reader that
using a JDBC DataSource directly in a servlet should probably be
considered poor architecture. I would recommend putting your
data-fetching logic in a support class, which will be easier to test and
maintain over time.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkwZYCEACgkQ9CaO5/Lv0PCi9QCeNZoqsj8I+H+1XI4tOWgzomyE
/FgAn1EqT6y+9v7Hbwcdt7n2yRo5mDCD
=GpeJ
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC
Resource)
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Gurkan,
On 6/16/2010 10:44 AM, Gurkan Erdogdu wrote:
> Please define some servlet and try with it,
>
> class TestClass extends HttpServlet{
>
> private @Resource(name="jdbc/TestDb") DataSource datasource;
>
> @PostConstruct
> public void postConstruct(){
> //Check datasource here
> }
> }
While this example appears to work, I might recommend to the reader that
using a JDBC DataSource directly in a servlet should probably be
considered poor architecture. I would recommend putting your
data-fetching logic in a support class, which will be easier to test and
maintain over time.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkwZYCEACgkQ9CaO5/Lv0PCi9QCeNZoqsj8I+H+1XI4tOWgzomyE
/FgAn1EqT6y+9v7Hbwcdt7n2yRo5mDCD
=GpeJ
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC
Resource)
Posted by Pid <pi...@pidster.com>.
On 16/06/2010 23:13, marble4u wrote:
>
> 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!
Find and read the Servlet Spec v2.5.
It's surprisingly easy to read for a technical Specification.
There are a number of interfaces that you can implement which will give
you interaction with the container but that don't depend on a request to
trigger them.
For example: the ServletContextListener, which has two methods one that
fires when the application starts and one that fires when it stops.
You could trigger the Timer in one, and cancel it in the other.
p
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC
Resource)
Posted by marble4u <ma...@energy4u.org>.
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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Resource Annotation has no effect but JNDI Lookup works (JDBC Resource)
Posted by Gurkan Erdogdu <gu...@yahoo.com>.
What is a TestClass? Tomcat has no knowledge about your class for injection occurs.
Please define some servlet and try with it,
class TestClass extends HttpServlet{
private @Resource(name="jdbc/TestDb") DataSource datasource;
@PostConstruct
public void postConstruct(){
//Check datasource here
}
}
--Gurkan
________________________________
From: marble4u <ma...@energy4u.org>
To: users@tomcat.apache.org
Sent: Wed, June 16, 2010 11:27:24 AM
Subject: 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
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>ResourceTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>testDb</description>
<res-ref-name>jdbc/testDb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
context.xml:
<Context path="/ResourceTest">
<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" />
</Context>
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 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.