SCJP 5.0<br />SCJD working on it
SCJP 5.0<br />SCJD working on it
SCJP 5.0<br />SCJD working on it
Mary John wrote:Hi all
I have a Map which holds list of all locked records as
private static Map<Long, Long> lockedRecords = new HashMap<Long, Long>();
Should I synchronise my lock method with the object lock on "lockedRecords"
ie with synchronised(lockedRecords), so that other methods( which may come in future enhancements )will not access lockedRecords while my lock method checks whether a particular record is present in it or not.
Or, is the code below which I have done with lock.lock() and lock.unlock() method just fine?
[ April 09, 2008: Message edited by: Mary John ]
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
Fola Fadairo wrote:There is a missed scenario in this implementation.
1. Thread A locks record 23.
2. Thread B attempts to lock record 23 and is placed in a waiting state.
3. Thread A deletes record 23.
4. Thread A releases the lock on record 23 and notifies Thread B.
5. Thread B locks record 23.
On step 5, a RecordNotFoundException should be thrown because record 23 was deleted while Thread B was waiting for the lock. The implementation does not perform this check. The check should be made again after the lock on a record has been obtained on line 21.
It is a subtle bug, but nonetheless a bug.
Regards.
P.S:
The validity of the record should also be checked before attempting to lock, i.e on entering the lock method.
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
Ulises Pulido wrote:
... what I do is to leave let the record locking for the row as you said, but I check if the row was deleted when I try to delete or update again launching a RecordNotFoundException not during locking.
I mean I perform that validation in the update and delete methods
...ops!<br>-----------------<br>
SCJD<br>
SCWCD 1.4<br>
SCJP 1.4
Alexandre Baldo wrote:
Ulises Pulido wrote:
... what I do is to leave let the record locking for the row as you said, but I check if the row was deleted when I try to delete or update again launching a RecordNotFoundException not during locking.
I mean I perform that validation in the update and delete methods
I did this too... but I don't think it is correct because we could hold a lock of a register that no longer exists...
I tried to re-check if the record was still valid before locking but I got a deadlock.
I don't know what to do.....
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
Ulises Pulido wrote:...
About the scenario that you mention what I do is to leave let the record locking for the row as you said, but I check if the row was deleted when I try to delete or update again launching a RecordNotFoundException not during locking.
I mean I perform that validation in the update and delete methods
Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
www.ulisespulido.com
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |