File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Alternative Locking Design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Alternative Locking Design" Watch "Alternative Locking Design" New topic

Alternative Locking Design

Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi Mark, Peter, Sai, Jason and others
Pls cosider the following locking design
Peter says leave the lock and unlock methods in empty and implement everything in LockManager a reference of which will be wrapped by each Connection object.
Problem here is leaving the methods empty. Data should atleast contain the info abt which rec no habe locked and which are free, if not the logic.
Mark says let each connection manage its own lock and then forward the request to which actually store the lock records (may be using a lock manager). Problem is here that each connection is managing which lock they have taken and thus at any point of time one can not know who have locked what
Here comes my view and design on it:
1) There will be a global lock manager which will have methods:
lock(recno, connection)
unlock(recno, connection)
It will contain a hashmap of recno-connection
2) every connection will get a reference of this manager from connection factory and lock(recno) method will delegate the lock request to this lock manager's lock(recno,this) and unlock(recno, this)
3) will contain a hashset containing the list of all the rec locked. It does not know who have locked which record. No lock manager here.
just 7-8 lines of code.
The methods in will contain notify and wait logic
3) While locking :
a. the lock manager will first check whether the client requesting for lock has already taken that lock or not. If not it will forward the request to lock(recno) in where actual locking will happen using wait() and notify(). Once through with this, the lock manager will then put the recno-connection object in the hashmap

3) While unlocking :
This lock manager will first check whether the client has actually taken the lock (by checking in hashmap) and if yes it will invoke the unlock(recno) method in After succesful invokation it will add the recno in its own hashmap.

lock(recno) and unlock(recno) in Data will just do the job of managing the threads using wait() and notify() and store the reco in its hashset.

Pls comment on this design
[ July 07, 2002: Message edited by: Amit Kr Kumar ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

6 of one, half a dozen of the other.
As far as what my design was, I now prefer the Peter way. I like the LockManager, which will knwo who locked what, and control the locking. With this in mind the Data class never has to know. You can have the Data class know, like in your design, I just see that as redundant now.
Good luck, and just remember as long as you justify your design in your design.txt, I am sure you will probably be fine in your design. I can't know for sure though.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

1) There will be a global lock manager which will have methods:

I don't like it when I see people attaching wordds "global" and "singleton" to lock manager. What will happen when a new database is added to the application? I think you will have less points taken off if your server is designed to handle multiple databases, and therefore multiple lock managers.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Mine would not handle multiple db files and I did not get any points deducted because of this.
Again this is an area of programmer preference, rather than one hard steady rule.
I agree. Here's the link:
subject: Alternative Locking Design
jQuery in Action, 3rd edition