Java EE using Datasource without search method

I am trying to use Datasource (Database) in my program using JNDI. I made it work partially, but not as if I want it to work. I want to add a JNDI resource, but it doesn't work as expected.

It doesn't work if I use annotation.

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

      

But it works when I use the search method:

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

      

I want to use annotation. I'm sure I need to set something in the web.xml, but I don't know what. I tried this, but I'm not sure which search engines to use.

+3


source to share


1 answer


Using resource annotation , you can try to use the lookup element instead of the name and use the jndi name that you configured on the app server.

EXAMPLE- If it matches the jndi name "jdbc / crmv1" try @Resource(lookup="dbc/crmv1")

either@Resource(lookup="java:jdbc/crmv1")



The only relationship with web.xml is that you use the deployment descriptor to override the resource mapping specified in the annotation.

+1


source







All Articles