views:

761

answers:

2

I'm someone that used to do some J2EE coding in the past and I'm coming back into the fold to work on a new J2EE project. A lot has changed since 2001, so I need to ask this very basic question.

I'm using syntax like this in my database class:

@Resource(name = "jdbc/MyDatabase")  
private javax.sql.DataSource dataSource;

I understand this is a new feature (annotations) but I'm not really sure how it works. Later on in my class I make this call:

Connection c = dataSource.getConnection();

And it throws a NullPointerException everytime. I step into this in the debugger and as it turns out, dataSource IS null.

It seems magical and weird that I do not initialize dataSource myself in the code. Any idea what I'm doing wrong?

My web.xml contains the following block to establish the resource:

<resource-env-ref>
    <resource-env-ref-name>jdbc/MyDatabase</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

And I've added this to my Context by using a context.xml file in META-INF. The Context entry looks like:

<Resource name="jdbc/MyDatabase" auth="Container" type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000"
    username="username" password="password" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/mydb?autoReconnect=true"/>

I'm using Tomcat 6, MySQL 5, the latest MySQL driver (not the older v3 but the newest v5 driver).

Update: This is not working for me because I'm using it in a plain-old class. I've created a new project and added a servlet to it. The DataSource is non-null in the servlet.

+2  A: 

Does your web.xml define the resource? Take a look at this example. Could you post your web.xml or the code that defines the jdbc/MyDatabase?

John Paulett
+2  A: 

You need at least to have the following in either the appserver's /conf/context.xml or the the webapplication's /META-INF/context.xml file:

<Resource
    name="jdbc/MyDatabase" 
    type="javax.sql.DataSource"
    driverClassName="com.your.Driver"
    url="jdbc:your://url"
    username="foo"
    password="bar"
/>

Also see http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html

And in the webapp's /WEB-INF/web.xml you need to define the following:

<resource-env-ref>
    <resource-env-ref-name>jdbc/MyDatabase</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

Edit: I now see that you edited your post to add the coding. I also see that you used resource-ref instead of resource-env-ref. The first refers to an external resource (read: not webcontainer-managed) and the second refers to an internal (environmental) resource (read: webcontainer-managed). Replace it and see.

BalusC
oh, i didn't know that @Resource removes the need to setup in web.xml. thanks!
the0ther
Sorry I confused the annotation, you actually need to define the resource in the web.xml. You only need to define it as `<resource-env-ref>` instead. Message edited.
BalusC
all my db code is buried in a layer implementing old-school type connection pooling...a Singleton DbPool class with no public constructor. that may be what's giving me trouble. i will tease it all apart and try a bare-bones shot at this.at what point does the private DataSource dataSource field become initialized to something non-null? does it matter if i use this in a class without a private constructor? does that member get automatically initialized the first time a constructor is called?
the0ther
Maybe the `@Resource` annotation isn't working properly. It should be responsible for getting the `DataSource` from the JNDI and assigning it to the variable. Does it work if you use the plain JNDI way? As to the Singleton DbPool you're talking about, it shouldn't be the cause of the problem, but at least this doesn't sound very good. You shouldn't be using a Singleton for that. Here's a good basic example of how to do it all properly: http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html Hope this helps.
BalusC
thanks a lot for the link i am going to have a look. i have created a bare-bones project just to work this out, i think i'll have it figured out very soon.
the0ther
and wow! can you get me a job there in Curaçao? sounds nice!
the0ther