Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this Mechanism for locking acceptable

 
Farouk Mohamed1
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Please can you comment on my locking schema:
1. Connection interface extends Remote (has a connect method which returns a proxy of RemoteData)
The connect method is used to connect to either local or remote database by a client ConnectionFactory.
2. ConnectionManager extends UnicastRemoteObject implements ConnectionInterface (Connection Factory for RemoteData objects)
3. RemoteDataInterface extends DataInterface
4. RemoteData class implements RemoteDataInterface extends UnicastRemoteObject
5. RemoteData class has a reference to Data class.
6. RemoteData class has a reference to LockManager Singleton and runnable which uses a synchronized hashmap to record information about locks(Key RecordNo,LockInformation).
7 LockInformation has a timestamp and the client RemoteObject which has the lock. so that using the timestamp a server thread which is runs Lock Managers run method cleans all dirty connection (Max time for record lock 60 sec and Database lock 30 sec).
8. The RemoteData class also has reference to singleton Lock Manager and locks and unlocks where implemented without signature change.
Please is this design correct and agreeable please please comment on this i have to decide this before i proceed
Regards
Farouk
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farouk, generally it looks very good.
Originally posted by Farouk Mohamed1:
schema:
1. Connection interface extends Remote (has a connect method which returns a proxy of RemoteData)
The connect method is used to connect to either local or remote database by a client ConnectionFactory.
If that means that the local Connection implementation (Data?) is instanceof Remote, that is a problem. If this is so, split the interface in a Connection (not extending Remote) and a RemoteConnection extends Connection, Remote { }.
6. RemoteData class has a reference to LockManager Singleton and runnable which uses a synchronized hashmap to record information about locks(Key RecordNo,LockInformation).
Rethink whether the HashMap needs to be synchronized. I'm willing to bet that, when you start coding your lock manager, you will find that its public methods need to be synchronized which renders synchronizing the HashMap unnecessary.
7 LockInformation has a timestamp and the client RemoteObject which has the lock. so that using the timestamp a server thread which is runs Lock Managers run method cleans all dirty connection (Max time for record lock 60 sec and Database lock 30 sec).
Investigate whether you can leverage java.rmi.server.Unreferenced in your design - you might or might not. The most bug-free, well-optimised and maintainable code is the code you don't write.
Success,
- Peter
 
Farouk Mohamed1
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Peter
Thanks for your reply

If that means that the local Connection implementation (Data?) is instanceof Remote, that is a problem. If this is so, split the interface in a Connection (not extending Remote) and a RemoteConnection extends Connection, Remote { }..

Excellent peter,
1 . Can you please comment on my new design after rethinking
ConnectionInterface throws RemoteException
RemoteConnectionInterface throws RemoteException extends Remote, ConnectionInterface
ConnectionManager implements RemoteConnection throws RemoteException.
So that the client Connection factory can have a single inteface for connection and can use polymorphism

Rethink whether the HashMap needs to be synchronized. I'm willing to bet that, when you start coding your lock manager, you will find that its public methods need to be synchronized which renders synchronizing the HashMap unnecessary.

2. You are right lock Manager has only 2 public methods in my design and both are synchronized so i think i can live with HashMap without synchronization. I would say the performance will be faster by using a non synchronized hashmap than a hashtable because we dont need synchronization on the containers becase the public methods are thread safe and synchronized;
Your comments please
3.

Investigate whether you can leverage java.rmi.server.Unreferenced in your design - you might or might not.

In my case the ConnectionManager is bound in the registry and it is a single object throughout the life of the server (Singleton even though the pattern is not implemented). The server holds only one reference of the ConnectionManager.
Question
1. Should the connectionManager implement unrefereced because it lives as long as the server lives as it gets bound int he registry when u start the server.
Please tell me your opinion
The connect method in the connectionManager creates new references of RemoteData for each client which is aswell a remote object.
I understand unreferenced is called by the remote server when there is no reference message issed by the client and server can then remove the weak reference object on the server.
I have tested and this works when clients sets the remote reference of RemoteData to null when he finishes things of.
Please reply your thaughts on
1. what if the client doesnt not set to null the remote reference? will the server call unreferenced after 10 mins(default).
2. Can I have a disconnect method on the ConnectionManager which takes a remoteReference and call explicit unreferenced() what should I do to kill the reference explicitly
I will appreciate your reply
Farouk
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first two points look fine Farouk.
I have tested and this works when clients sets the remote reference of RemoteData to null when he finishes things of.
Please reply your thaughts on
1. what if the client doesnt not set to null the remote reference? will the server call unreferenced after 10 mins(default).
2. Can I have a disconnect method on the ConnectionManager which takes a remoteReference and call explicit unreferenced() what should I do to kill the reference explicitly
One answer for both, really - I would expect that the client calls close() on the connection when he's finished. In local mode, this simply delegates to Data.close(). In remote mode, the call performs locking cleanup and disconnects. So the Connection.close() method would render an explicit disconnect() on the ConnectionManager unnecessary.
Unreferenced is simply a safety net that cleans up when a client dies unexpectedly with no chance to close() - it is not a replacement, as you note you don't want locks hanging around for 10 minutes if you can avoid it.
- Peter

[This message has been edited by Peter den Haan (edited October 16, 2001).]
 
Farouk Mohamed1
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks peter i appreciate your help
I understand that the only way to lock records for update is up to client to lock read modify unlock.
1. I have changed modify in remote mode in such a way that if the record is not locked then a exception is thrown back to client asking him to lock before modification hope it should be alright
2. Peter because I split my connection Interface into RemoteConnectionInterface extends Remote and implemented by Connection manager ,
3. I had to create a localConnection object implement the ConnectionInterface and hold a reference to LocalData which holds a reference of Data Class
4. All this is to have a factory in the client to create a remoteconnection or localconnection and aswell a remote data use polymorphism in connection and data access . Do you think this is necessary and ok
Please i appreciate your comments.
Farouk
 
Farouk Mohamed1
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter please can you have a look at my last reply
many Thanks
farouk
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patience... There's nothing blatantly wrong with your design as far as I can see. Maybe you can consider if you really need the connection factory in local mode - after all, it is the polymorphism at the Data level that is most important. I'm not saying you're on the wrong track! As long as you know (and can defend) why you're doing it the way you are.
- Peter
 
Farouk Mohamed1
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks peter
Sorry for rushing again, i dont know how many times i am gonna ask sorry
Smiles
Farouk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic