Java Reference
In-Depth Information
Chapter 5
Business Logic Using EJB
The EJB 3.2 specification has been included as part of the Java EE 7 release, and with it will come more enhancements
to further solidify the Java enterprise platform. Although the EJB 3.2 release does not include any radical redesigns,
it helps improve developer productivity and flexibility by adding a few new features and bringing the specification
into alignment with other EE 7 technology updates. The updates include minor enhancements to the transactional
life cycle, bean passivation, timers, and more. New features have been added to EJB Lite, a subset of features for those
who do not need the entire EJB stack. Better alignment with the Java Message Service (JMS) has also been added,
making it even easier to configure message-driven beans (MDBs). Lastly, a handful of features that are rarely used,
or induce overhead, have been pruned from the release to make it even easier to use. This chapter will focus on each
area of the updates and show examples of how to utilize the latest features.
General Updates and Improvements
Many of the changes made in the EJB specification for 3.2 are considered incremental updates and improvements.
That said, these features are a nice addition and will certainly come in handy in certain situations. This section
will cover these enhancements.
Transaction Life-Cycle Callbacks in Session Beans
Session beans have callback methods that are invoked when certain stages of a bean's life cycle occur. For instance,
a method can be registered within a session bean via annotation to invoke after the bean is constructed
( @PostConstruct ), before it is destroyed ( @PreDestroy ), and so on. Sometimes it makes sense to start a new
transaction when one of these events occurs. It is possible to specify the transactional status of an annotated
life-cycle callback method within a session bean, when using container-managed transactions. In the following
excerpt from org.javaee7.jpa.session.AcmeFacade , a new transaction is started upon creation of the bean and
also upon destruction of the bean:
@Stateful
public class AcmeFacade {
@PersistenceContext(unitName = "IntroToJavaEE7PU", type = PersistenceContextType.EXTENDED)
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@PostConstruct
public void init() {
System.out.println("The Acme Bean has been created");
}
 
Search WWH ::




Custom Search