ejb@glassfish.java.net

Re: Defining DataSource JNDI name in ejb-jar.xml

From: Cheng Fang <Cheng.Fang_at_Sun.COM>
Date: Tue, 29 Aug 2006 09:23:52 -0400

If you need to declare a resource-ref in ejb-jar.xml, then annotation in
java code doesn't simplify anything. In that case, I would suggest you
not use @Resource in bean class.

To use <resource-ref> in ejb-jar.xml, it's the same as in previous
versions of ejb. Your FacadeBean will need to lookup the declared
resource:

DataSource ds = (DataSource) sessionContext.lookup("jdbc/my-ds");

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<enterprise-beans>
  <session>
    <ejb-name>FacadeBean</ejb-name>
    <business-remote>com.xxx.FacadeRemote</business-remote>
    <ejb-class>com.xxx.FacadeBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>
      <resource-ref>
        <description>description</description>
        <res-ref-name>jdbc/my-ds</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>
 </session>
</enterprise-beans>


Cheng




AKostylev wrote:

>Hello!
>
>I have a stateless bean with DataSource field:
>public class FacadeBean implements Facade
>{
> @Resource(name = "jdbc/logicTestIP") private DataSource ds;
>...
>
>When using it in such way all works fine.
>But I want to define JNDI name ("jdbc/logicTestIP") for the DataSource in ejb-jar.xml not in java
>code.
>
>How can I do this? What will look like my FacadeBean and ejb-jar.xml?
>
>Thank you.
>
>
>
>