Java EE using Datasource without lookup method

957 views Asked by At

I'm trying to use a Datasource (Database) in my program using JNDI. I got it to work partially but not like I want it to work. I want to inject the JNDI Resource, but it does not work properly.

It does not work, if I use the annotation.

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

But it does work, when I use the lookup method:

try {
        ds = InitialContext.doLookup("jdbc/crmv1");
    } catch (NamingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

I want to use the annotation. I'm pretty sure, that I have to set something in the web.xml, but I don't know what. I tried to google it, but I'm not really sure what searchterms to use.

1

There are 1 answers

5
Laurentiu L. On BEST ANSWER

Using the Resource annotation you can try using the lookup element instead of the name and use the jndi-name you configured in your application server.

Example- If it is mapped with the jndi-name "jdbc/crmv1" try @Resource(lookup="dbc/crmv1") or @Resource(lookup="java:jdbc/crmv1")

The only relation to the web.xml is that you use a deployment descriptor to override the resource mapping that you specify in an annotation.