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



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

2010-06-17 Thread Gurkan Erdogdu
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 marco.blev...@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-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)

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


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)

2010-06-16 Thread Gurkan Erdogdu
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 marco.blev...@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


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

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-16 Thread Pid
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




signature.asc
Description: OpenPGP digital signature


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

2010-06-16 Thread Christopher Schultz
-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-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-16 Thread Gurkan Erdogdu
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 users@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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org