Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

There are several possible ways to refer to it, as follows.

Injected Name Match

The BY matching variable name actually matches the configured name of the resource.to resource name

Code Block
java
java
@Stateless
public class FooBean {
    @Resource DataSource myDerbyDatabase;
}

Name Attribute Match

The name attribute of the @Resource annotation matches the configured name of the resource.OR BY matching name

Code Block
java
java
@Stateless
public class FooBean {
    @Resource(name="myDerbyDatabase")
    DataSource dataSource;
}

JNDI ENC Injection

We inject the resource into the JNDI environment using the name attribute of the @Resource to match the configured name of the resource.OR BY JNDI lookup

Code Block
java
java
@Resource(name="myDerbyDatabase", type="javax.sql.DataSource")
@Stateless
public class FooBean {

    public void setSessionContext(SessionContext sessionContext) {
        DataSource dataSource = (DataSource) sessionContext.lookup("myDerbyDatabase");
    }

    public void someOtherMethod() throws Exception {
        InitialContext initialContext = new InitialContext();
        DataSource dataSource = (DataSource) initialContext.lookup("java:comp/env/myDerbyDatabase");
    }
}

...