You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by mean_bcp <go...@versatile.com> on 2015/11/18 16:29:31 UTC

Resource lookup from EJB

I'm having a problem very similar to
http://tomee-openejb.979440.n4.nabble.com/JNDI-Global-resources-and-EJB-JAR-td4663113.html,
however I am not explicitly creating Threads.

I'm using a MDB in the ejb.jar, and within the MDB attempting to do a DB
resource lookup which is failing. This same resource lookup works from
within my web app which is deployed in the same ear. I have tried to declare
the resource in a resources.xml file, and when that failed, declared it in
tomee.xml.

The lookup code below is common code used for both the web app and the EJB.
It works in one place, but not the other. Logs show that the webapp finds
the resource in comp/env, while the EJB does not find it in either.

                Context initCtx = new InitialContext();
		String dsName = props.getProperty("db.datasource");
		DataSource d = null;
		try {
			String s = "java:comp/env/" + dsName;
			log.info("Looking up datasource named " + s);
			d = (javax.sql.DataSource) initCtx.lookup(s);
			log.info("Successfully found datasource named " + s);
		} catch (NamingException e) {
			String s = "java:global/" + dsName;
			log.info("Looking up datasource named " + s);
			d = (javax.sql.DataSource) initCtx.lookup(s);
			log.info("Successfully found datasource named " + s);
		}

Do you have any suggestions to make this work?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Resource-lookup-from-EJB-tp4676840.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Resource lookup from EJB

Posted by Romain Manni-Bucau <rm...@gmail.com>.
the same kind of binding exists for EJB and would have made it working
in the MDB, that said since you configure a name to build the jndi
lookup name, configuring the full name is as simple IMHO.

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2015-11-18 8:07 GMT-08:00 mean_bcp <go...@versatile.com>:
> Thank you for the quick response. The lookup by openejb:Resource/${dsName}
> worked in both spots! (Although java:openejb/Resource/${dsName} threw a
> NameNotFoundException)
>
> Maybe bound to comp/env because I had this tag in my web.xml (now removed):
>
> <resource-ref>
>           <res-ref-name>dolleeDb</res-ref-name>
>           <res-type>javax.sql.DataSource</res-type>
>           <res-auth>Container</res-auth>
>           <res-sharing-scope>Shareable</res-sharing-scope>
>   </resource-ref>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/Resource-lookup-from-EJB-tp4676840p4676843.html
> Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Resource lookup from EJB

Posted by mean_bcp <go...@versatile.com>.
Thank you for the quick response. The lookup by openejb:Resource/${dsName}
worked in both spots! (Although java:openejb/Resource/${dsName} threw a
NameNotFoundException)

Maybe bound to comp/env because I had this tag in my web.xml (now removed):

<resource-ref>
	  <res-ref-name>dolleeDb</res-ref-name>
	  <res-type>javax.sql.DataSource</res-type>
	  <res-auth>Container</res-auth>
	  <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Resource-lookup-from-EJB-tp4676840p4676843.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Resource lookup from EJB

Posted by Romain Manni-Bucau <rm...@gmail.com>.
How did you bind dsName in comp/env - this is not the default?

A quick solution for you is to configure the full jndi path instead of just
the ds name and lookup openejb:Resource/dsName or
java:openejb/Resource/dsName.
Le 18 nov. 2015 07:44, "mean_bcp" <go...@versatile.com> a écrit :

> I'm having a problem very similar to
>
> http://tomee-openejb.979440.n4.nabble.com/JNDI-Global-resources-and-EJB-JAR-td4663113.html
> ,
> however I am not explicitly creating Threads.
>
> I'm using a MDB in the ejb.jar, and within the MDB attempting to do a DB
> resource lookup which is failing. This same resource lookup works from
> within my web app which is deployed in the same ear. I have tried to
> declare
> the resource in a resources.xml file, and when that failed, declared it in
> tomee.xml.
>
> The lookup code below is common code used for both the web app and the EJB.
> It works in one place, but not the other. Logs show that the webapp finds
> the resource in comp/env, while the EJB does not find it in either.
>
>                 Context initCtx = new InitialContext();
>                 String dsName = props.getProperty("db.datasource");
>                 DataSource d = null;
>                 try {
>                         String s = "java:comp/env/" + dsName;
>                         log.info("Looking up datasource named " + s);
>                         d = (javax.sql.DataSource) initCtx.lookup(s);
>                         log.info("Successfully found datasource named " +
> s);
>                 } catch (NamingException e) {
>                         String s = "java:global/" + dsName;
>                         log.info("Looking up datasource named " + s);
>                         d = (javax.sql.DataSource) initCtx.lookup(s);
>                         log.info("Successfully found datasource named " +
> s);
>                 }
>
> Do you have any suggestions to make this work?
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Resource-lookup-from-EJB-tp4676840.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>