tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brianm709 <>
Subject DataSource lookup by JNDI name
Date Fri, 17 Apr 2009 16:57:35 GMT

I have seen several posts with similar issues, but haven't found the solution
to my issue.

I am running openEJB 3.1 embedded in eclipse.  I am using it to test an EJB.

In my test I define my datasource like:

    Properties props = new Properties() ;
    props.setProperty( Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory" ) ;
    props.put("jdbc/awddb", "new://Resource?type=DataSource");
    props.put("jdbc/awddb.UserName", "sa");
    props.put("jdbc/awddb.Password", "password");
    InitialContext initialContext = new InitialContext(props) ;

I can then immediately access the datasource in the test code with the
following lookup.
    DataSource ds = (DataSource) new

My goal is to access the DataSource by JNDI name inside my EJB.
I have tried a number of combinations.

The only solution I have found that works is:
I include the following in my ejb-jar.xml for the EJB

Then in the EJB I access it using the following:
    EJBContext ejbContext = (EJBContext) new InitialContext().lookup(
"java:comp/EJBContext" ) ;
    DataSource ds = (DataSource) ejbContext.lookup( "jdbc/awddb" );

Is it possible to lookup the DataSource without including the resource-ref
in ejb-jar.xml?  I have a number of EJBs, and I would prefer to not
duplicate this for each one.

Ideally I would be able to access the DataSource using the following from
any EJB.
DataSource ds = (DataSource) new InitialContext().lookup( "jdbc/awddb" ) ;


View this message in context:
Sent from the OpenEJB User mailing list archive at

View raw message