wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Resource Injection on DataSource throws NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Resource Injection on DataSource throws NullPointerException" Watch "Resource Injection on DataSource throws NullPointerException" New topic
Author

Resource Injection on DataSource throws NullPointerException

Debojit Sinha
Ranch Hand

Joined: Mar 13, 2010
Posts: 66

Hi, I'm trying to use Resource Injection with Datasource to connect to my database, but when I try to get the conection from the DataSource object, I get a NullPointerException. The JNDI entry is working fine, because I was able to fetch data when I used DataSource with JNDI name lookup, but not using @Resource. Could anybody point out what I'm doing wrong? The complete code is as follows(NullPointerException occurs for lines 21 and 26, and I'm using JDeveloper 11g and WebLogic 10.3.4):

Any suggestion would be much appreciated.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Debojit,
I assume you are on JEE 5? That version doesn't let you inject resources into random classes; only JEE managed entities. This article is about Oracle's solution, but describes the problem nicely.

I'm going to move your thread to our JEE forum since it more about dependency injection than JDBC.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Debojit Sinha
Ranch Hand

Joined: Mar 13, 2010
Posts: 66

Hi Jeanne,
Thanks for the reply, it was most helpful, but in that case, how do I go about abstracting the database connection aspect from the business code? Can you suggest any workaround? The article you mentioned did suggest one (I think), but I couldn't make much sense of it, sorry.
Debojit
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Debojit Sinha wrote:Hi Jeanne,
Thanks for the reply, it was most helpful, but in that case, how do I go about abstracting the database connection aspect from the business code? Can you suggest any workaround? The article you mentioned did suggest one (I think), but I couldn't make much sense of it, sorry.
Debojit

The article suggests an Oracle specific API. I wasn't proposing you use it; I just like their description of the problem.

Approaches that will work:
1) Use JNDI directly
2) Inject the resource in your servlet/ejb and pass it to your class
3) Use Spring .

I personally favor #1 over #2. You can still have a separate factory class that abstracts the InitialContext/JNDI logic from your business class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resource Injection on DataSource throws NullPointerException
 
Similar Threads
Using a jsf DAO interface
Using datasource for login bean & login page?
An illegal attempt to commit a one phase capable resource with existing two phase
Looking For Connection Bean For Connection Pool
ORA600 error with jdk1.4, ojdbc14.jar and oracle10.2.0.0