(URLyBird 1.3.2)
I have an different approach. I explain my whole strategy, i hope it will help you with your locking issues.
I'm using RMI and therefore i have an exposed Business Object (RoomManger) these Object enables an RMI client to call the methos listAll, book, search. These are the major functionalities.
The are no locking issues at the client side. The locking issues are all taken at the server side within RoomManager, if ClientA wants to book a record. The book method first aquires a lock on the record. I'm using ReentrantReadWriteLock since this is part of
Java 1.5. The locks are managed in a big collection where a lock for each record exists. (Managed in the DBMain implementation).
Now lets continue. The book method locks the record. Then it does an update an then it releases the lock. A lock is only be held for one single method call from the client-side view.
This makes your life very simple, since there can be no deadlocks, and no lost locks. Locks can only be lost if the server crashes and if this happens the server has to be restarted and everthing works fine again.
Therefore in the implementation of DBMain the update, create or find method do not have do deal with locking. This must be done one layer up, by the caller of this DBMain implementation. Thats the way i understand the requirements, otherwise the lock and unlock method would not make any sense.
In my implementation it would look like this:
Client A request all Records
Server locks all Records
Server reads all Records and return them
Server unlocks all Records
Client B request all Records
Same as before ...
Client A books record 5
Client B books record 5
Call from Client A is processed a little bit faster and it first receives the thread-safe lock of record 5
Call from Client B reaches the critical section, but the lock is already held by Client A
Client B waites ...
Client A continues
The book method checks if the room is not booked by someone else.
Client A updates the record and unlock the record and returns.
Client B is now available to continue
The book method checks if the room is not booked by someone else.
The check failes and an error is returned to the client.
Now it does make sense that in the case of an error, the Client refreshes all displaying records --> then Client B sees that Client A has already booked the record.
The data of the JTable on the clientside could be dated out, but if it is not everthing works fine, it's a little optimistic strategy. If two clients try to book at the same time one will fail.
I hope that has helped you.