Package ejava.examples.txhotel.ejb
Class HotelReservationSessionEJB
- java.lang.Object
-
- ejava.examples.txhotel.ejb.HotelReservationSessionEJB
-
- All Implemented Interfaces:
HotelReservationSession
,HotelReservationSessionLocal
,HotelReservationSessionRemote
,SessionSynchronization
public class HotelReservationSessionEJB extends Object implements HotelReservationSessionLocal, HotelReservationSessionRemote, SessionSynchronization
This class provides an example of a stateful session bean wrapper around stateless server calls. In this example case, the user wishes to make several reservations. However, the stateless session bean will commit them to the database one at a time. This wrapper shows how someone could temporarily store them on the server while the entire set is being sanity checked. Once they are good to go, a single transaction is started by the commit() method to invoke the stateless session bean for each transaction. We can easily find tons of fault and areas for improvement with the actual business logic. However, the basic concept is that the stateful session bean can perform some of the actions of the stateless session bean outside of a transaction and then commit the changes as one unit when ready. The instance is thrown away no matter what happens. We could just as easily keep it around and add some edit functions to fix the erroneous information that caused the rollback. This bean is deployed 3 times; default configuration, using a transaction Required stateless session bean, and using a tranaction RequiresNew stateless session bean. The test client will try all 3. You'll notice that the one with RequiresNew, will not be able to rollback successful reservations because it operates in a separate transaction. The one with tranaction Required allows all to be rolled back because it joins this tranaction. This bean is also configured quite a bit from the ejb-jar.xml file; using less annotations then the other stateless session bean example. It implements the javax.ejb.SessionSynchronization interface so that it can listen, impact, or react to the state of the active transaction.
-
-
Field Summary
Fields Modifier and Type Field Description private SessionContext
ctx
private HotelReservationSession
impl
private static org.slf4j.Logger
log
private HotelRegistrationLocal
reservationist
-
Constructor Summary
Constructors Constructor Description HotelReservationSessionEJB()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterBegin()
This is called to tell us the transaction has started because we implement SessionSynchronization interface.void
afterCompletion(boolean status)
This is called to tell us how the transaction did.void
beforeCompletion()
This is called to give us a chance to complain before the transaction really commits.void
cancelReservations()
void
close()
This method will destroy the stateful instance.void
closing()
This method is called just prior to the bean being destroyed.List<Reservation>
commit()
void
createReservation(Person person, Date startDate, Date endDate)
void
init()
This method is called when the bean is instantiated to perform manual initialization.
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
-
impl
private HotelReservationSession impl
-
ctx
private SessionContext ctx
-
reservationist
private HotelRegistrationLocal reservationist
-
-
Method Detail
-
init
@PostConstruct public void init()
This method is called when the bean is instantiated to perform manual initialization. It creates the business logic implementation class, instantiates and assigns a DAO, and assigns the EntityManager from the DAO(s) to use. This should map the the ejb-jar.xml post-construct element.
-
closing
@PreDestroy public void closing()
This method is called just prior to the bean being destroyed. This should map to the ejb-jar.xml pre-destroy element.
-
close
public void close()
This method will destroy the stateful instance.- Specified by:
close
in interfaceHotelReservationSession
-
createReservation
public void createReservation(Person person, Date startDate, Date endDate) throws HotelReservationException
- Specified by:
createReservation
in interfaceHotelReservationSession
- Throws:
HotelReservationException
-
cancelReservations
public void cancelReservations() throws HotelReservationException
- Specified by:
cancelReservations
in interfaceHotelReservationSession
- Throws:
HotelReservationException
-
commit
public List<Reservation> commit() throws HotelReservationException
- Specified by:
commit
in interfaceHotelReservationSession
- Throws:
HotelReservationException
-
afterBegin
public void afterBegin()
This is called to tell us the transaction has started because we implement SessionSynchronization interface.- Specified by:
afterBegin
in interfaceSessionSynchronization
-
beforeCompletion
public void beforeCompletion() throws EJBException, RemoteException
This is called to give us a chance to complain before the transaction really commits.- Specified by:
beforeCompletion
in interfaceSessionSynchronization
- Throws:
EJBException
RemoteException
-
afterCompletion
public void afterCompletion(boolean status)
This is called to tell us how the transaction did.- Specified by:
afterCompletion
in interfaceSessionSynchronization
-
-