On Thu, Aug 26, 2004 at 01:38:51AM +0200, Jacek Laskowski wrote:
> Mark Lybarger wrote:
> 
> >is it possible for me to configure openejb to make available a
> >datasource outside of using a resource-ref?
> 
> You don't have to use resource-ref. You can declare 
> InitialContextFactory as the Local or Remote one of OpenEJB and use the 
> stuff bound to it. Resource-refs are the way to abstract a code from the 
> real container of these refs. Using resource-ref allows you to make 
> changes to resource configuration outside of your code (no recompilation 
> necessary then).
> 
> Just attach itself to OpenEJB JNDI via the Local or Remote 
> InitialContext (see http://www.openejb.org/embedded.html or 
> http://www.openejb.org/remote-server.html) and lookup what's available 
> casting where required to the appropriate type, e.g. javax.sql.DataSource.
> 

A couple corrections: using resourc-refs is the only J2EE or EJB
compliant way to get a DataSource from an enterprise bean; datasources
aren't available through either the Local or Remote InitialContext
implementations.

The compliant way is to do this:


    <resource-ref>
        <res-ref-name>my/favorite/datasource/foo</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>



These are all valid ways to get that datasource.

-----------------
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) 
ctx.lookup("java:comp/env/my/favorite/datasource/foo");
-----------------
        Context ctx = new InitialContext();
        ctx = (Context) ctx.lookup("java:comp/env");
        // ...
        DataSource ds = (DataSource) ctx.lookup("my/favorite/datasource/foo");
-----------------
        Context ctx = new InitialContext();
        ctx = (Context) ctx.lookup("java:comp/env");
        // ...
        ctx = (Context) ctx.lookup("my/favorite");
        // ...
        DataSource ds = (DataSource) ctx.lookup("datasource/foo");
-----------------


Everything the bean looks up from JNDI must be relative to
"java:comp/env" in one way or another.

-David

Reply via email to