tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <david.blev...@visi.com>
Subject Re: DataSource lookup by JNDI name
Date Tue, 21 Apr 2009 03:43:09 GMT

On Apr 17, 2009, at 9:57 AM, brianm709 wrote:

>
> 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.

A note that in the 3.1.1 release lookups to "openejb:Resource/jdbc/ 
awddb" will work inside a bean and outside a bean and regardless of  
how the InitialContext was created. Definitely though the <resource- 
ref> or @Resource is the more preferred and portable approach.

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

Adding this to your EJB would work as well:

  @Resource
  private DataSource awddb;

Note the variable name is significant, we use it to help us figure out  
which datasource to give you when we construct the EJB.  If you wanted  
to use any variable name you can try this:

  @Resource(name = "awddb")
  private DataSource dataSource;

Or

  @Resource(name = "jdbc/awddb")
  private DataSource dataSource;

All of those will work on superclasses as well, so if you have a  
common super class you can put it there.

In terms of the internal name you've chosen for your DataSource, I'd  
recommend simply calling it "awddb" like so:

    props.put("awddb", "new://Resource?type=DataSource");

I know that will work with the above three injection styles and is  
well tested.  I'm not sure, but I suspect adding the "jdbc/" prefix  
might break the first two, though the last one should definitely work.

-David


Mime
View raw message