aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Can I throw exception in LOCK/UNLOCK 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 "Can I throw exception in LOCK/UNLOCK" Watch "Can I throw exception in LOCK/UNLOCK" New topic
Author

Can I throw exception in LOCK/UNLOCK

amit ahuja
Ranch Hand

Joined: Nov 20, 2001
Posts: 38
Hi all,
I was worried abt a small thing.. Can i throw some LockException in Lock(int) and unlock(int) methods in my extended data class, considering the requirement as below ??
"Record locking must be implemented using the methods public void lock(int) and public void unlock(int)."
amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Why would you have a "Lock" exception in lock in the Data class. If you are using a LockManager, than have that class handle any exceptions.
If your code in the lock method of Data class needs to throw an exception, then I'd re-check your design.
Mark


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

Joined: Nov 20, 2001
Posts: 38
Mark,
I do have lock Exception in LockManger only.
But at same time I throw IOException from extended data class for primarily two cases
1.) when the record is not available..(required as comment in provided Data class on lock method.
2.) when the thread is interrupted by calling disconnect from the client. In that case i dispose off the (i assume it is one only at a given time) locked record from that client.
what do u feel abt this design?
wud lov to know ur view.
amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

1.) when the record is not available..(required as comment in provided Data class on lock method.
2.) when the thread is interrupted by calling disconnect from the client. In that case i dispose off the (i assume it is one only at a given time) locked record from that client.

1. Where is this requirement. I don't remember seeing any. What do you mean by not available. Do you mean another client has that record locked. That is what the wait() method is for, to wait until the other client unlocks the record.
2. Why and how would a disconnect from the client be sent in the middle of it's own transaction of lock and unlock? Doesn't make sense.
Mark
amit ahuja
Ranch Hand

Joined: Nov 20, 2001
Posts: 38
Mark, answer to first question is this piece of code from my data class....I'm talking abt "The record position is invalid"
/**
* Lock the requested record. If the argument is -1, lock the whole
* database. This method blocks until the lock succeeds. No timeouts
* are defined for this.
* @param recno The record number to lock.
* @exception IOException If the record position is invalid.
*/
public void lock(int record) throws IOException {
}
and why i had disconnect is that, suppose the wait time is very high (where i show busy cursor), that time the user can disconnect from the menu.
Also if user closes his window when a record is locked, I call this disconnect (which is basically dispose off all acquired locks by that client).
I'm sure it is not any critical requirement but at same time it was easy to implement.
what u think mark
amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well, I always look at it this way. How long will it take to lock and unlock the record that you are modifying. With one user it is like a millisecond, so now you add a second client, so that client is requesting the same record, so it now has to wait. How long? a millisecond. Do you need to go through the whole implementation of making sure that the user doesn't close his window? I don't feel it should.
As far as a client disconnecting when he has locked records. This is possible, but from previous threads in this forum, you will find out about an interface called Unreferenced, this interface will handle all of this for you. You have your remote implementation of the DataAccess interface implement Unreferenced, include the single method in that interface, and in that method, loop through the client's locked records, and unlock them. Now you no longer need to put needless code into your lock and unlock methods.
Mark
amit ahuja
Ranch Hand

Joined: Nov 20, 2001
Posts: 38
Mark, you are right on Unreferenced interface.
but that doesn't fit into my design, because i have only one remote object serving all clients.
And now even if one client goes down, the unrefernced() method won't be called till all the clients die.
Infact in my design if one client locks and get disconnected. That record is always locked till all clients disconnect, thus unrefernced() method called and in which releaseAllLocks() called.
Mark do you think this will make me loose points? if yes, please tell me how to take care of stale locks in my design.
Thanks
amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well since I didn't use that design, I am not completely sure of the best way to do this. I had thought of many ways to try it when I was working on the assignment, but none of them would work. But then I say the "Connection" method of each client having their own remote object, then the design became very easy and a lot more clear.
I know there is probably some way to deal with stale locks with some sort of timer that kicks off when that record gets locked, and then after a time period, check to see if the client has unlocked it, if it hasn't then you could assume that the client is lost, and then unlock that record.
I do think that the second way I just wrote probably takes more tiem and effort to write, then to change you design to include the Connection object.
Mark
sanjeev mehra
Ranch Hand

Joined: Feb 12, 2001
Posts: 93
Hi!
Can I have concept of LockManager class. How it works? I will be very thankful to you.
Regards,
Sanjeev.
amit ahuja
Ranch Hand

Joined: Nov 20, 2001
Posts: 38
Sure sanjeev you can have LockManager. It is cleaner to do that way.
And it just a class which keeps track of record no. and the conncetion who locked. It is called by Lock(recordno) method and unlock(recordno) from extended data class.
do i answer ur query?
amit
Forrest Xu
Greenhorn

Joined: Jan 30, 2002
Posts: 26
Hi,
How about for lock(int record) when record = -2 or
record > record count. In this case, do we need
to throw some exception like DatabaseException?
amit ahuja
Ranch Hand

Joined: Nov 20, 2001
Posts: 38
Yes u have to throw exception but IOException.. check out the comment on lock method in given Data.java
amit
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can I throw exception in LOCK/UNLOCK