Hi folks,
Ive been trying to understand just how logical record locking is supposed to prevent two uers/clients from booking the same record, hence overwriting each other. Below is some background info about my assignment after which I'll lay out the scenario which Im trying to figure out.
Assignment Info
-----------------
URLyBird 1.1.1
Signature and comments for lock method:
// Locks a record so that it can only be updated or deleted by this client.
// Returned value is a cookie that must be used when the record is unlocked,
// updated, or deleted. If the specified record is already locked by a different
// client, the current thread gives up the CPU and consumes no CPU cycles until
// the record is unlocked.
public long lock(int recNo) throws RecordNotFoundException;
Signature for unlock method:
// Releases the lock on a record. Cookie must be the cookie
// returned when the record was locked; otherwise throws SecurityException.
public void unlock(int recNo, long cookie)
throws RecordNotFoundException, SecurityException;
Problem scenario (without logical record locking)
Consider two clients A and B.
1.) Client A retrieves all records and this is diplayed on the screen in the JTable
2.) Client B retrieves all records and this is diplayed on the screen in the JTable
3.)Client A selects record 1 and client B also selects record 1 (this record's owner field is empty so both clients can tell from there that the record is available)
4.) Client A attempts to book record 1 and in so doing calls the update method (directly or indirectly, yet to decide) in Data class. I intend to synchronize access to the update method so that when client A's thread enters the update method no other thread can enter until client A releases the Data object's lock.
5.) Similarly, Client B attempts to book record 1 but cannot proceed because client A has not finished executing the synchronized update method in the Data class. So Client B will block until Client A finishes the method and releases the lock (note that in the update method, i'll check whether the record is still available before updating the record)
6.) After Client A finishes the update method and releases the Data object's lock, Client B attempts to continue and enters the synchronized update method to perform its booking. However, when it checks whether the record is still available, it finds that it is no longer available and so it can't book it. So the thread returns and the client is notified that the record is no longer available.
My question:
The whole point of logical record locking is to prevent two or more clients from booking the same record and hence overwriting eachother's booking. However, from the scenario I have above, I dont see how two clients can overwrite eachother's booking because of the check i'll introduce in my update method to see whether the record is still available. Is there something Im missing here?
*I have not done any coding
Cheers!
Ive been trying to understand just how logical record locking is supposed to prevent two uers/clients from booking the same record, hence overwriting each other. Below is some background info about my assignment after which I'll lay out the scenario which Im trying to figure out.
Assignment Info
-----------------
URLyBird 1.1.1
Signature and comments for lock method:
// Locks a record so that it can only be updated or deleted by this client.
// Returned value is a cookie that must be used when the record is unlocked,
// updated, or deleted. If the specified record is already locked by a different
// client, the current thread gives up the CPU and consumes no CPU cycles until
// the record is unlocked.
public long lock(int recNo) throws RecordNotFoundException;
Signature for unlock method:
// Releases the lock on a record. Cookie must be the cookie
// returned when the record was locked; otherwise throws SecurityException.
public void unlock(int recNo, long cookie)
throws RecordNotFoundException, SecurityException;
Problem scenario (without logical record locking)
Consider two clients A and B.
1.) Client A retrieves all records and this is diplayed on the screen in the JTable
2.) Client B retrieves all records and this is diplayed on the screen in the JTable
3.)Client A selects record 1 and client B also selects record 1 (this record's owner field is empty so both clients can tell from there that the record is available)
4.) Client A attempts to book record 1 and in so doing calls the update method (directly or indirectly, yet to decide) in Data class. I intend to synchronize access to the update method so that when client A's thread enters the update method no other thread can enter until client A releases the Data object's lock.
5.) Similarly, Client B attempts to book record 1 but cannot proceed because client A has not finished executing the synchronized update method in the Data class. So Client B will block until Client A finishes the method and releases the lock (note that in the update method, i'll check whether the record is still available before updating the record)
6.) After Client A finishes the update method and releases the Data object's lock, Client B attempts to continue and enters the synchronized update method to perform its booking. However, when it checks whether the record is still available, it finds that it is no longer available and so it can't book it. So the thread returns and the client is notified that the record is no longer available.
My question:
The whole point of logical record locking is to prevent two or more clients from booking the same record and hence overwriting eachother's booking. However, from the scenario I have above, I dont see how two clients can overwrite eachother's booking because of the check i'll introduce in my update method to see whether the record is still available. Is there something Im missing here?
*I have not done any coding
Cheers!