File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Can I recover from a optimistic lock exception within the same transaction? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can I recover from a optimistic lock exception within the same transaction? " Watch "Can I recover from a optimistic lock exception within the same transaction? " New topic
Author

Can I recover from a optimistic lock exception within the same transaction?

Max Tomlinson
Ranch Hand

Joined: Jul 17, 2001
Posts: 365
Hi All-

I want to use Hibernate�s optimistic locking but I want to be able to recover from a lock exception within the same transaction � is this possible?

The scenario:

We have a hierarchy of records that may be updated by multiple users during periods of high activity. These are total records and when a user is trying to update a stale record, I want to:

1)catch the optimistic lock exception
2)retrieve my original data (in user cache) and calculate deltas
3)find the latest record (which caused the optimistic lock exception � I do a flush after every save so it�s current)
4)add the deltas to the new record
5)save the updated record

The problem I�m having is at step 3. I can�t find my record because the transaction no longer exists and my session is dead. I�ve read that Hibernate errors like this are non-recoverable. Can I do this? Or do I have to implement my own user version checking to check for stale data (i.e. finding the record before I save) in order to recover programmatically?

(We�re using JBoss 4 , Seam + Hibernate)

thanks
Max
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


3) find the latest record (which caused the optimistic lock exception � I do a flush after every save so it�s current)

Well, its current within the scope of your transaction (depending on your level of transaction isolation). It will not actually be the latest record after an optimistic lock failure.

The only way I can think of doing something like this is to cache your Hibenrate data seperately from Hibernate, watch for the concurrent update exception, reload the data into the session, reapply your changes (you will have to consider if the ywill they still be valid) and retry the transaction.

Not easy, and quite a big overhead to fix what should be a very occasional issue. If you are seeing lots of lock exceptions you might find it easier to redesign bits of your application so the business transactions have a smaller scope.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Max Tomlinson
Ranch Hand

Joined: Jul 17, 2001
Posts: 365
Hi Paul-
thanks for the reply:
question - you said
'reload the data into the session, reapply your changes (you will have to consider if the ywill they still be valid) and retry the transaction.'

This is what I want to do. How do I reload the current record (the one that superseded the one I was about to update)? I get a sql error when I try to do a find due to a broken transaction.
thanks
Max
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You do it from outside the scope of your current transaction.

Perhaps you can post some code of what you have tried? Might make a discussion a bit easier.
Max Tomlinson
Ranch Hand

Joined: Jul 17, 2001
Posts: 365
The code is all over the place
essentially, what I am doing is:

1. catch stale object exception
2. try to find the record again using entityManager (to get the latest copy)

this find fails because there is no transaction.

I suspect I will have to write my own optimistic locking code because we are running ejb3 within seam and this is a seam defined transaction which was exited by the stale object exception. Normally when the user experiences this kind of error the page is exited and they are taken to a generic 'please try again' page. In my case, I want to repair and keep going. I don't think Seam and Hibernate are going to let me. We are not at a point in our app development where we can redefine transactions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I recover from a optimistic lock exception within the same transaction?