This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes help on lock and unlock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "help on lock and unlock" Watch "help on lock and unlock" New topic
Author

help on lock and unlock

harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
HI all,
This is my current bookflight method in my Data.java
public synchronized boolean bookFlight(int recordNumber,Object clientID,int seatsToBeBooked){
this.clientID = clientID;

lock(recordNumber);
seek(recordNumber);
read();
modify(dataInfo);
unlock(recordNumber);
if success return true else return false;
}
This is my lock() in Data.java

public void lock(int recordNumber){
synchronized(lockManager){
lockMagager.lock(recordNumber,clientID);
}
}

public void unlock(int recordNumber){
synchronized(lockManager){
lockManager.unlock(recordNumber,clientID);
}
}

I LockManager class is implemented also.
The above code works great to handle multiple clients trying to book seats in one flight.Even if i am not synchronizing on lock manager object, since the bookflight method is synchronized and allows only one client at a time to do lock,read,modify,unlock operation, we have no danger of second client even get into the book flight method,until the first finishes up its work and release the lock.i understand that according to the requirements ,lock and unlock
has to be implemented,i am trying to figure out their exact usage for this application,because with the above approach,only one client operates in this method, there is no need to lock and unlock the record in the above code.i am missing some thing here, need some comments.
thanks
harcharan
[ October 21, 2002: Message edited by: harcharan kanwal ]
[ October 21, 2002: Message edited by: harcharan kanwal ]
[ October 22, 2002: Message edited by: harcharan kanwal ]
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
Originally posted by harcharan kanwal:


It's good to post any code inside of CODE tags (see the buttons at the bottom of your message window).
Also see (using the URL button, to save the time of typing out the tags)
Implementing Lock/Unlock (2 pages total) and other such stuff.


Fly by Night Consultants<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr><i>I climbed on the back of a giant albatross<br />which flew through a crack in the cloud<br />to a place where happiness reigned...<br />all year 'round<br />the music played ever so loudly!</i><p><a href="http://thomasfly.com/songs/Traffic/Hole_in_my_Shoe_qt.htm" target="_blank" rel="nofollow">Hole in My Shoe</a><hr></blockquote>
Juan Katabasis
Ranch Hand

Joined: Jun 20, 2001
Posts: 46
i should not implement such a bookflight method in Data.java. actually i should not implement anything related to application specifics in that class. even lock and unlock can be discussed to be implemented in Data as it can be implemented in different ways (optimistic, pesimistic, etc).
you should take a look at older messages, where great discussions were made about lock-unlock topics .


Regards<br />J.
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
thanks guys,
recordNumber
noOfSeats
clientID
i think all above three are required in the server to perform bookflight operation and locking a record to per client connection.could you please elaborate more on your suggestion of having client code on the server side.i can change the Random object to some int object for the clientID.please respond
thanks
[ October 21, 2002: Message edited by: harcharan kanwal ]
[ October 22, 2002: Message edited by: harcharan kanwal ]
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi all,
i have read lot of posts on lock and unlock
my question is still at large,
1.As long as bookFlight methos is synchronized at the method level,only one client can enter the method and does its work and leave,there is no need to even have a lock at the lock manager.
2.If bookFlight mehod is not synchronized,you allow multiple clients to enter in the method,they all will have different values of
recordNumber
numberOfSeatsToBeBooked
clientID
So the second approach will not work,no matter if you synchronized on lockManager or synchronize on any part of the code of this mehtod (synchronize on the part of the code is not recomended)

please respond.
correct me what i am missing. need to know ,if there is any hidden role of lock and unlock methods
thanks,
harcharan
[ October 22, 2002: Message edited by: harcharan kanwal ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Where is your bookFlights method. It should not be in your Data class or you LockManager which are the only two classes that multiple clients can be accessing at the same time.
And you only wnat to synchronize on the last object possible to keep deadlocks from happening.
In our FBN case that is the Collection that holds the list of the locked records, and when you synchronize on that it keeps it for a short time, either through completion or Thread.wait().
Does this make sense?
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
Where is your bookFlights method. It should not be in your Data class

it is in Data.java.i think of it as a basic database operation that a client like to perform in this applicaion.
And you only wnat to synchronize on the last object possible to keep deadlocks from happening

what exactly do you mean by last possible object,is it modify(),if it is , then in the second choice that i mentioned one post above will get multiple clients , and they all will carry different values of the parameters of this method and cause the deadlock situation

In our FBN case that is the Collection that holds the list of the locked records, and when you synchronize on that it keeps it for a short time, either through completion or Thread.wait().

yes, i have implemented hashMap to hold key value pair of recordNumber and clientID hashMap .
thanks mark
harcharan
[ October 22, 2002: Message edited by: harcharan kanwal ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Do all databases have a bookFlights method? no they do have a save, but it is generic. bookFlights is more specific to this application so it needs to be in a place that is specific. Like your DataAccessFacade, basically the "middle tier" business logic layer. It is not part of the GUI or the Data, so it is the business logic.
What I mean by last object to synchronize, I am talking about the HashMap, in the lock or unlock methods, not in modify.
Mark
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
Thanks for Reply Mark,i am coming back to this post little late
Do all databases have a bookFlights method? no they do have a save, but it is generic. bookFlights is more specific to this application so it needs to be in a place that is specific. Like your DataAccessFacade, basically the "middle tier" business logic layer. It is not part of the GUI or the Data, so it is the business logic.

yes,your comments makes sense to me.I am adding bookFlight methos to my DataClient class,which implements all the public methos of the Data.java and supports the local and remote connection, and it has other application and gui specific methods in it.I think this will work.your comments
What I mean by last object to synchronize, I am talking about the HashMap, in the lock or unlock methods, not in modify.[/QUOTE

The following is my LockManager class

Lock manager class get instantiated in the Data class,and lock and unlock methods are implemented as it is in the topmost post of this thread.

i have the following confusion
what is multiple clients access
1.is it when i synchronized the bookFlight method , and allow only one client at a time to enter to lock ,read ,modify and unlock operation.but then i think where is the need for to lock the record ,when i am not allowing another client to even enter the method body of bookFlight.
2.if i do not synchronized on the bookFlight at the method level, then i am allowing multiple clients to enter the in the methods,but then with each client enterance ,it brings in different values of clientID,numberOfSeatsToBeBooked,recordNumber.This will cause the corruption of the data or deadLock,no matter what part of the method is synchronized.
The only thing that works right now is the solution number 1 i.e synchronized at the method level,but then to me it does not seems like multiclient opertion or it is.
put some light on it, i am stuck thinking of it.
thanks
harcharan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
On the method where I check if the record is locked and where a record gets locked I synchronize on this, not the method. But the requester have to wait() for a whort while before checking again if the record is locked, beeing in a while (true) loop the requesting thread can eventually get the lock and proceed with modify(). The realeaseLock() method is however synchronized.
HTH
Cheers
Torgny
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: help on lock and unlock
 
Similar Threads
why is it necessary for unLock() to check clientID?
my choice on lock/unlock.help me to decide,Eugene and Andrew
Best way to test multiple bookings
lock/unlock
My Lock Design !