EJB and concurrence
My question is about concurrence.
If I have two different users, accessing the same page, modifying the same
register in database. One of both will save with success, and the other?
How can I treat this situation? Is possible avoid the problem or generate
some specific exception saying that the register was modified, using EJBs?
Re: EJB and concurrence
I am not sure what you are asking exactly. So I'll take different scenarios
where multiple users are modifying the same data.
1) Multiple users have a "view" of the same data from the database. The
user who updates "last" wins. It does not matter at what point a user got
the view. Previous update by any user gets "trashed" by latest update.
This is default behavior with or without EJBs. It does not involve anything
special on your part
2) Multiple users have a "view" of the same data. Data consists of parts
A and B. User 1 reads AB, User 2 reads AB, User 1 Modifies AB to A'B, User
2 modifies AB to AB'. Resulting contents in database are AB' from the second
user. The "correct" requirement is to have the database contents look like
A'B'. You have two choices to make this happen. Again this is with or without
EJBs. But I will cite EJBs since you asked.
2.1) Pessimistic locking: The client either explicity, or the server on behalf
of the client locks the record in the database when a view is presented to
the client. The client modifies the data and sends the data back to the
server which updates the database. All OTHER clients trying to access that
data are "held up" because of the database lock. If the duration of the
transaction is very small, this may be acceptable. Reality dictates otherwise.
This is inherently a BAD idea. Which brings us to the next and more desirable
2.2) Optimistic locking: Your database table has an extra column which contains
information on when data was last updated. The easiest way is to have a
timestamp - a LONG type will do which will store a millisec value. In your
Entity Bean when you load the data at the server you also load this timestamp.
The data is delivered to one/more clients. When a client saves data, the
timestamp is updated in the Entity Bean and since bean is transactional timestamp
is also updated in the database. So how does it work? When data is saved
it is done so by updating the entity bean in the server with the client's
"view" of the data. The "view" has its own timestamp. If the timestamp
matches the timestamp in the bean, you know no one has updated the data in
the interim. If the timestamp in the bean is different - or I should say
later - than that in the incoming client view, you resolve the actual data
elements that have changed. This way you can keep the changes consistent
due to updates from multiple users.
Finally, I will say, it all depends on what you want. I assumed you asked
the question because you were worried of losing updates to data by one user
because another came along and wiped it out with stale data in its own view.
"Robson" <firstname.lastname@example.org> wrote:
>My question is about concurrence.
>If I have two different users, accessing the same page, modifying the same
>register in database. One of both will save with success, and the other?
>How can I treat this situation? Is possible avoid the problem or generate
>some specific exception saying that the register was modified, using EJBs?
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL