javadesign-patternsejb-3.0session-bean

Simple but good pattern for EJB


What would you suggest as a good and practical but simple pattern for a solution with:

And is it necessary to use an own layer of DAO for persistence? I use JPA to persist objects to my DB.

Should I withdraw business logic from my EJB? All online sources tell me different things and confuses me...


Solution

  • I would definitely put the business logic in Stateless Session Beans. Stateless session beans are nice as they nicely capture the transaction boundaries. And it decouples the View layer from the persistence layer.

    Take care that the methods of the SSB correspond to small business goals the user wants to achieve.

    Another point is that you must be sure that the data you return has all data in the object tree and that you do not rely on lazy loading to get the rest, because this causes all kind of problems.

    Stay as far away as possible from Stateful Session Beans : they are bad news and are a broken concept in the context of a web application.

    For long running things, consider using Message Driven Beans which you trigger by sending a JMS message. These are a nice way to do background processing which frees the business logic faster, keeps transactions shorter and returns control to the end user faster.