This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Diferent ways to locate ejbs from POJOs  RSS feed

 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i recently sent a post about JNDI issues, this post is not the same as the other one altough it's about the same topic. I found out the problem on the other post!!

My question is pretty simple. After i spent some hours trying to figure out the problem in my application, i need some more answers.

In a situation the i have a Fa�ade -> POJO -> CMP, in other words, a situation where the fa�ade delegates the business implementation to a POJO, what are the diferent manners this POJO can access the CMPs???

So far, i found out 2 diferent ways, but in some of this methods i need some explanation

First option - If i set a reference (in ejb-jar.xml and jboss.xml) for SessionOneBean to CMPOneBean, i can access CMPOneBean from the POJO with a lookup such that.



Well, this is clear to me, i understand fine.


Second option - If i DON'T set a reference (in ejb-jar.xml and jboss.xml) for SessionOneBean to CMPOneBean, i can access CMPOneBean from the POJO with a lookup such that.



Why exactly does it work??? I don't get it!!!
If i try the lookup wit the "java:comp/env/...." stuff it doesn't work in this case.


Third option - If i just i don't set references and also lookup for the remote interface withoud the "java:comp/env/ejb/..." sutff it also works.



Finally, why when i use the "java:comp/env/ejb/..." stuff, i cannot locate ejbs from POJOs??? Is it because the COMP enviroment is not accesible to POJOs???
 
Brian Tinnel
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use "java:comp/env/xxxx" the lookup only uses the ejb-ref/ejb-local-ref entries. So the xxxx must define an ejb-ref'ejb-local-ref entry. The ejb-ref entries are located based on the current context you are running in when you create your InitialContext. If you call a POJO from SessionBeanA and it does a new InitialContext() it will get the env entries defined for SessionBeanA. If you then call the same POJO from SessionBeanB and it again does a new InitialContext() it will now have the env entries defined for SessionBeanB. I have seen some people who try to store the InitialContext in a POJO's static field. As you can see, that could easily cause a problem.

If you don't use "java:comp/env/xxx", the name you enter must be the exact jndi name. Since the exact jndi name doesn't change from one bean to another, this way will work in all cases. The major problem is that it requires a code change if the jndi name changes, or if the ejb-link (part of ejb-ref/ejb-local-ref) changes because you move a bean from one jar to another.

It is normally a good idea to use the "java:comp/env/xxxx" method. Then you use the ejb-ref/ejb-local-ref to define the bean you are referring to. That way if the jndi name changes, you only need to change the deployment descriptor.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand it now!
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!