users@glassfish.java.net

Re: How to find a Datasource by using Resource-Reference ?

From: Shalini Muthukrishnan <Shalini.Muthukrishnan_at_Sun.COM>
Date: Tue, 25 Aug 2009 22:13:24 +0530

Hi Veronika,

If you have created a jdbc-connection-pool called myPool and a jdbc
resource referencing this pool as jdbc/myres, the following would be the
entries in the descriptors. Also, the myPool could be pinged from the
admin console.

ejb-jar.xml :

<ejb-jar>
<enterprise-beans>
<session>
      <ejb-name>SimpleSessionEJB</ejb-name>
      <home>com*...........SimpleSessionHome</home>
      <remote>com*.............SimpleSession</remote>
      <ejb-class>com*..........SimpleSessionBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
      <resource-ref>
        <res-ref-name>DataSource</res-ref-name> ---------> name
you are giving to the resource.
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>
    </session>
 </enterprise-beans>


sun-ejb-jar.xml :

<sun-ejb-jar>
  <enterprise-beans>
    <ejb>
      <ejb-name>.........</ejb-name>
      <jndi-name>com*...........SimpleSessionHome</jndi-name>
      <resource-ref>
        <res-ref-name>DataSource</res-ref-name> ------> same name you
gave in ejb-jar.xml earlier.
        <jndi-name>jdbc/myres</jndi-name> ------> name of
the jdbc resource you created for the pool.
      </resource-ref>
    </ejb>
 </enterprise-beans>
</sun-ejb-jar>


In your ejb, if you do a lookup of
DataSource ds = (DataSource) ic_.lookup("java:comp/env/DataSource");

the datasource object related to the jdbc/myres would be got and that
would inturn refernce the jdbc connection pool "mypool". There is no
need to specify the pool name in the descriptors.

HTH,
Shalini.

glassfish_at_javadesktop.org wrote:
> Hi there,
>
> I hope this is not too bad noob question :)
>
> I have question about how to configure glassfish to find a Datasource using ejb-jar and sun-ejb-jar. The descriptors can be seen in the Admin Console.
>
> Here's the ejb-jar:
>
> <ejb-jar>
> <enterprise-beans>
> <session>
> <ejb-name>****</ejb-name>
> <home>com.....****Home</home>
> <remote>com.....***Remote</remote>
> <ejb-class>com......****</ejb-class>
> <session-type>Stateless</session-type>
> <transaction-type>Container</transaction-type>
> <resource-ref>
> <res-ref-name>jdbc/myDataSource</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <res-auth>Container</res-auth>
> </resource-ref>
> </session>
> </enterprise-beans>
> </ejb-jar>
>
> and here the sun-ejb-jar:
> <sun-ejb-jar>
> <enterprise-beans>
> <ejb>
> <ejb-name>***</ejb-name>
> <jndi-name>***</jndi-name>
> <resource-ref>
> <res-ref-name>jdbc/myDataSource</res-ref-name>
> <jndi-name>myPool</jndi-name>
> </resource-ref>
> <pass-by-reference>false</pass-by-reference>
> </ejb>
> </enterprise-beans>
> </sun-ejb-jar>
>
>
> As you can see, I have a resource-ref to something called myDataSource, which I want to use myPool.
> "myPool" is existent (it's a JDBC resource), I can ping the corresponding ConnectionPool and make a lookUp.
>
> But if I want to make a lookup on "myDatasource" I always get nothing. Here's what I tried:
>
> dataSource = (DataSource) ctx.lookup("myPool"); --> this works!
> dataSource = (DataSource) ctx.lookup("myDataSource");
> dataSource = (DataSource) ctx.lookup("jdbc/myDataSource");
> dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/myDataSource");
>
> What am I doing wrong? Is it a programming issue or a configuration issue?
>
> Thank very much
>
> Veronika
> [Message sent by forum member 'tesis_glassfish' (vbrem_at_tesis.de)]
>
> http://forums.java.net/jive/thread.jspa?messageID=361834
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>