Is it allowed to pass-through an ejb references to a used class of an ejb?

I have an EJB3 stateless session bean that uses some other ejbs to do its work. As I do not want to have all the logic in the ejb class itself, I created some classes which solve the problems. These classes are called by the ejb. To have the referenced ejbs in these classes I created a context object that holds the injected ejb references which is passed to the classes.

My question: is this allowed to store ejb references in simple object fields or must I use some special ejb reference fields? At first glance I can’t see a reason why because everything is handled in one thread an transaction.


You can pass these EJB references and store them in these auxiliar classes without any risk.

Nevertheless, EJB specification encourages you to implement all the logic of your system through EJBs. So, from EJB design philosofy, you should declare these “auxiliar” classes as EJBs also (they perform business logic in the end) and inject into them the needed EJBs references to reduce the coupling (specially if the former EJB doesn’t even use the injected EJBs).

Again, if you don’t do it and just pass the references manually, no technicall issue should arise.

Leave a Reply

Your email address will not be published. Required fields are marked *