tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brianm709 <bmmer...@dstsystems.com>
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.JdbcDriver",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
    props.put("jdbc/awddb.JdbcUrl",
"jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;DatabaseName=db");
    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
InitialContext().lookup("java:openejb/Resource/jdbc/awddb");

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
      <resource-ref>
	<res-ref-name>jdbc/awddb</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
      </resource-ref>

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" ) ;

Thanks.

-- 
View this message in context: http://www.nabble.com/DataSource-lookup-by-JNDI-name-tp23101598p23101598.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Mime
View raw message