Sometimes when working with an object, a developer will want to lock a
collection of objects. For example, assume the following conditions:
1. An NBA team has to live under a salary cap.
2. Team management may be modifying two players salaries simultaneously.
3. They submit their changes at the same time.
In reality, the business rule that must be checked at submittal relates to the
player's salary increase busting the salary cap. This is prevented by checking
the teams collective salaries and not allowing the post to succeed if the
business rule is violated.
Without a locking mechanism in place (or some other design consideration), it
will always be possible for the above scenario to fail. If both submit requests
ask for the total salary cap before either one writes to the database, than both
commits have the possibility of succeeding. This can happen even if the business
rule is violated.
An elegant solution is to create a team lock object that must be checked out
prior to obtaining the salary total for a team, and is released after the
database write. By allowing only one process a lock at a time solves the
problem. This can easily be done using Java's threading capabilities and the
wait/notify mechanism.
Now for the question...
How can this mechanism be implemented in EJB?
It seems like I would need a Singleton Session Bean, although this goes against
the concept of a session bean as every client would have to access the same
object to guarantee proper locking.
Since there is a single lock object, I tended to look at an Entity Bean. Would
it be good design (as related to the salary cap example) to make a Finder method
that goes into a wait state if the entity bean is currently locked by another
process? This seems to be where I am leaning.
Is it possible to take advantage of EJB transactions to prevent the salary cap
business rule violation? I don't think this is a transaction problem as much as
a locking issue...
Any thought or comments?
jim
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".