After assembling an Enterprise JavaBeans (EJB) module that contains container-managed
persistence (CMP) beans, you can prevent certain types of database-related
exceptions from occurring during application run time. Using sequence
grouping, you can specify the order in which entity beans update
relational database tables.
Eliminate exceptions resulting from referential integrity
(RI) violations
Sequence grouping is particularly useful
for preventing violations of database
referential integrity (RI).
A database RI policy prescribes rules for how data is written to and
deleted from the database tables to maintain relational consistency.
Run-time requirements for managing bean persistence, however, can
cause an EJB application to violate RI rules, which can cause database
exceptions. These run-time requirements mandate that:
- Entity bean create and remove operations correlate to the database
immediately upon method invocation.
- Entity bean changes are cached by the EJB container until either
a finder method is called, or the transaction ends.
Consequently, the order in which entity beans update the database
is unpredictable. That randomness translates into high risk of the
application violating database RI. Although caching the operations
for batch processing overrides these run-time requirements, it does
not guarantee a bean persistence sequence that follows any given RI
policy.
The only way to guarantee a persistence sequence that
honors database RI is to designate the sequence, which you do in the
EJB deployment descriptor editor of the assembly tool. Through the
sequence grouping feature, you assign beans to CMP groups. Within
each group you specify the order in which the persistence manager
inserts bean data into the database to accomplish updates without
violating RI.
See the Setting the run time for CMP sequence groups topic
for detailed instructions on designating sequence groups. Consult
your database administrator about the RI policy with which you need
to synchronize.
Minimize exception risk for optimistic concurrency
control schemes
Sequence grouping can also reduce the risk
of transaction rollback exceptions for entity beans that are configured
for
optimistic concurrency control.
In these concurrency control schemes, database locks are held for
minimal amounts of time so that a maximum number of transactions consistently
have access to the data. The relatively unrestricted state of the
database can lead to transaction rollback exceptions for two common
reasons:
- When concurrent transactions attempt to lock the same table row,
database deadlock occurs.
- Transactions can occur in an order that violates application logic.
Use the sequence grouping feature to order bean persistence
so that these scenarios are less likely to occur.