Hello everyone
In my implementation I have decided to use RMI and have a DataServer class that is displays a simple GUI and binds my remote connection factory to the registry.
The GUI displays log and error messages fom the server and has 2 menu options namely 'Help' and 'Shutdown'.
Each time a client requests a data connection from the remote connection factory, the factory will check its current collection of Data and LockManager pairs to see if it already holds references to the requested filename's instances and if so returns a new remote connection object constructed with the relevant pair. Otherwise it instantiates the pair, stores them for future use and returns a new remote connection instance with the newly instantiated pair.
This means that my server will require no changes if the client application needs to access more than one data file in future which is highly likley.
The difficulty is knowing how to handle the shutdown of the server to make sure all locks for all files have been released. I realise this is above and beyond the scope for the assignment but its bugging me and I really want to find a good solution.
Currently when the user selects shutdown the DataServer calls close on the connection factory that does the following:
1) Prevents any new connections being made - returns an IOException if a connection is requested after close.
2) Cycles through each of the LockManagers for each of the data files opened, waits for all their locks to be released then locks the whole
file with -1.
I don't like though as it is very conceivable that a client will in future need a reference to more than one file and will want to perform updates on each. If the client locks a record in data file A and does an update and unlocks the record, then tries to lock a record in data file B to update that then unlocks it; and the DataServer has meanwhile called close() on the connection factory which is merrily locking whole data files one after the other and has managed to lock data File B before data File A then the client will sit there trying to get its lock for the second update on file B forever.
Worse still the server has no guarentee that the client won't lock a record in file A and try to lock a record in file B before trying to unlock its record in file A such that the connection factory will find itself in a deadlock situation where it has locked file B and is trying to lock file A but the client cannot unlock its record in file A without getting the lock on its record in file B.
How have people handled this shutdown situation? I basically somehow need to establish when there are no locks on any of the files and at that moment prevent any further locks on all files simultaneously - but how to do this is alluding me at this stage
This is easily done withmore than one file but how about many?
Any ideas would be more than appreciated
Thanks Sam