Java Reference
In-Depth Information
// Commit when all tasks succeed
ut.commit();
} catch (Exception ex) {
try {
//Some tasks failed. Rollback.
ut.rollback();
} catch (SystemException syex) {
throw new EJBException("Rollback failed: " + syex.getMessage())
}
throw new EJBException("Transaction failed: " + ex.getMessage());
}
}
The preceding code snippet demonstrates the usage of
UserTransaction
methods. The
begin
and
commit
invocations delimit the updates to the database. If the updates fail, the code invokes the
rollback
method and throws an
EJBException
.
To summarize the discussions in this section,
Table 20-3
lists the types of transactions allowed for the
different types of EJBs.
Table 20-3:
Allowed Transaction Types for EJBs
EJB Type
Container-
Managed
Transaction
Bean-Managed
Transaction
JTA Transaction
JBDC Transaction
Entity
Allowed
Not Allowed
Not Allowed
Session
Allowed
Allowed and
Recommended
Allowed but not
Recommended
Message Driven
Allowed
Allowed and
Recommended
Allowed but not
Necommended
Summary
This chapter provides a brief introduction to the vastly broad area of EJB. You learn how to develop
EJBs by writing a simple session bean:
HelloEJB
. You also study a stateful session bean,
YachtSessionEJB
. The following specific topics are discussed:
Session beans
Entity beans
Message-driven beans
EJB transactions
In the
next chapter
, you will learn about the entity beans using bean-managed persistence.