I have some confusion on synchronization and locking, so I suppose this is as good a
thread to post them in as any. First of all let me introduce you to my classes:
ClientToken: a token issued to the client when it first connects to the server. In all subsequent get/update calls to the server the token is passed as an argument to the server to uniquely identify the client.
DataFile: singleton class that maintains an in-memory cache of records as well as being responsible for reading and updating the data file.
RecordService: implements IRecordService which implents Remote. Provides methods for creating/updating/deleting records. In the implementation each method takes a ClientToken as an argument, as well as the necessary Record information. The methods then instantiate a DataAccess object, passing in the ClientToken to the constructor. The appropriate create/update/delete method is then called.
DataAccess: implements the DBMain interface. There is also a private static inner LockManager class (a single instance for all DataAccess objects). The lock() method takes a client token and a record number, identifying which client has locked which record.
No one else seems to have used a design like this, and I'm beginning to wonder if I've missed something important. At the moment only 3 of my DataAccess methods contain synchronized blocks; the lock(), unlock() and isLocked() methods which both synchronize on the single instance of the LockManager. So they look like:
I don't
think anything else needs to be synchronized at all. Can anyone see any flaws with this?
Also, I have just realised that if 2 threads update the owner field on the same record, then (assuming the first thread gets the lock first) the owner field will be updated twice and the first update will be lost. Obviously this is a problem. How did other people solve this? I'm trying to keep buisiness logic out of the data access layer, so I don't really want to explicitly check in the update method if a record has been booked or not before I update that particular part of the
String array. Any thoughts?
Jon