Skip to main content

Dependency injection of datasource in servlet is not working

4 replies [Last post]
mustangdolphin
Offline
Joined: 2006-03-24
Points: 0

Dependency injection of datasource in servlet is not working for me i am getting a null pointer exception while trying to getConnection() on the ds object
this is what i have written in the servlet.This i tried on the glassfish-b41 build.

@Resource(name="jdbc/MyDataSource")
private DataSource ds;

i tried with "mappedName" attribute also that too did't work, whereas am able to lookup the datasource using the InitialContext like this
public void doGet(...) {
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("jdbc/MyDataSource");
}
}

the same i did with session bean there also i get the same problem i could not get datasource/sessionbean thru dependency injection.

this is the error iam getting, i dont know where i went wrong pls correct me if iam wrong.

java.lang.NullPointerException
at TestServlet.doGet(TestServlet.java:19)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:423)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:278)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:179)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:182)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:120)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:137)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.invokeAdapter(ProcessorTask.java:667)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.processNonBlocked(ProcessorTask.java:574)
at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:844)
at com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:287)
at com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:212)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
at com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:75)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ludo
Offline
Joined: 2003-06-10
Points: 0

Do you have something like this in your web.xml?

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

Injection is only working for Java EE 5 application, so your web application needs to be servlet 2.5 compliant.

Ludo

mustangdolphin
Offline
Joined: 2006-03-24
Points: 0

Thank you very much dude...that works...

ss141213
Offline
Joined: 2005-03-30
Points: 0

There is something [b]wrong[/b] here. Adding version="2.5" to web.xml [b]must not[/b] make a difference. You should be able to use dependency injection in your servlet even if your web.xml contains version="2.4". This is a bug in the container. The version attribute in web.xml is not the version of the Java EE(or J2EE) spec the application is written against. It is just the schema version of the web.xml. I have just filed a P2
https://glassfish.dev.java.net/issues/show_bug.cgi?id=506
to track this issue.

Thanks,
Sahoo

ss141213
Offline
Joined: 2005-03-30
Points: 0

I was wrong in my earlier comment. The bug has been closed. Pl refer to the bug where you can find some very good information.

As I mentioned in the bug's closing comment, I am not convinced that this was good decision.

Thanks,
Sahoo