File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock(-1)... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock(-1)..." Watch "lock(-1)..." New topic
Author

lock(-1)...

Andrea Gazzarini
Ranch Hand

Joined: Sep 09, 2002
Posts: 125
I'm confused with these two questions...
What happens if I have two blocked record and another incoming thread calls data.lock(-1)??
Who is this thread?? (that is, how the user can lock all the db ?? By prenoting all the flights.. (no, because the requirements says: "The user can book one or more seats on 'a' specif flight", so it is supposed the booking function makes reservation on one flight at time.
Greetings


Regards,<br />Andrea<br /> <br /><a href="http://www.linkedin.com/in/andreagazzarini" target="_blank" rel="nofollow">Andrea Gazzarini</a><br />Skype ID : gazzax72<br />Google Talk ID : a.gazzarini@gmail.com<br /><a href="http://www.andreagazzarini.blogspot.com" target="_blank" rel="nofollow">My Blog</a>
Vikas Sood
Ranch Hand

Joined: Sep 03, 2002
Posts: 109
Hi Andrea,
According to specs with me and my understanding of the locking,U will have to wait till all the record locks are removed before locking for the whole database( -1).
For further understanding ,u can search this forum for various threads about locking and lockmanger
VikasSood
Andrea Gazzarini
Ranch Hand

Joined: Sep 09, 2002
Posts: 125
Thanks Vikas.
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Andrea,
and you should also take care that there won't be a third thread coming in and locking some other record while your lock (-1) thread is waiting!


Mag
Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
Mag,
Do you think that is a requirement? Or can we just document in our design choices that the lock(-1) is used for maintenance purposes only, and should therefore only succeed if no more clients are using the database (even though the lock(-1) may starve).
Is that "good enough", or should we add an additional semaphore to prevent new clients from obtaining any locks once a lock(-1) is executed?
Thanks!
--Dave.
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Dave,
I don't know. I've read posts from several people that implemented this case, too, and so did I. I think the locking is the most tricky part of the assignment.
Actually it's not very difficult (but it requires some thinking). I used two booleans to steer through this problem, databaseLockRequested and databaseLocked.
Hope this helps.
Hope this helps.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Dave Teare:
[...]Is that "good enough", or should we add an additional semaphore to prevent new clients from obtaining any locks once a lock(-1) is executed?
Such a semaphore would lead to deadlock if any client needed more than one lock at a time. No, the FBN application doesn't, but remember this is a reusable, generic bit of database code.
The implementation was good enough for my assessor.
- Peter
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
But is there something that the user can do that could lock the database? As far as my understanding goes such a thing need not be performed from the client side. I think this is for maintainence purposes too...
Please correct me if I a wrong..
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi,
IMHO the locking mechanism requires the developer to define some behaviour. I found that there are several ways of solving this problem, but each is based on a different set of assumptions regarding how the locking mechanism is supposed to work.
These assumptions include: the number of locks per client (more than one lock allowed?), in which situations lock (-1) will be called (e.g. maintenance, server shutdown), etc. I think you have to make your assumptions clear (for example in the javadoc comments), and provide an implementation that works for these assumptions.
Hope this helps.
James Lee
Greenhorn

Joined: Nov 24, 2002
Posts: 11
Is it important for the server to unlock the locked records for the client who disconnects communication with server side?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I would say it is required for a production system. Whether it is required for the assignment is debatable. I have seen people who had not catered for this pass with with fine scores, so it is obviously not essential.
- Peter
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Peter,
quote:
--------------------------------------------------Originally posted by Dave Teare:
[...]Is that "good enough", or should we add an additional semaphore to prevent new clients from obtaining any locks once a lock(-1) is executed?
-----------------------------------------------
quote:
--------------------------------------------------
Originaly posted by peter : "Such a semaphore would lead to deadlock if any client needed more than one lock at a time. No, the FBN application doesn't, but remember this is a reusable, generic bit of database code."

I think incase of db locking its better to make the server to wait lock(-1)(database locking) till the existing locks get executed and mean while we should also Block the new lock requests which comes after the database lock is called(-1). If possible in the client side we can popup a message saying DataBase locked by throwing an exception.
I feel this wont lead to deadlock even if any client needed more than one lock at a time. Because once the existing locks got executed the server will got exit, where we will popup a message in the clientside.
Correct me if iam wrong.
regards,
-rameshkumar
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Ramesh kumaar:
I think incase of db locking its better to make the server to wait lock(-1)(database locking) till the existing locks get executed and mean while we should also Block the new lock requests which comes after the database lock is called(-1).
What if a client (not an FBN client, obviously) does And between the first and the second line, another client attempts to acquire a database lock?
If possible in the client side we can popup a message saying DataBase locked by throwing an exception.
In that case you are relying on your clients performing proper cleanup:This is quite a lot to ask, many developers aren't quite as conscientious as that. Alternatively, you could move this cleanup activity into the database by redefining the semantics of lock() so that a client loses all its locks when lock() throws an Exception.
Obviously, this is all extra complexity in the API and the server implementation. Against this, you have to weigh what you expect the lock(-1) facility to be used for. I figured it is intended for maintenance of some kind, and that it would not be unreasonable for such maintenance to wait until a moment that all clients had released their locks. Combine this with Sun's explicit instruction to keep things simple and it is clear what the course of action ought to be.
You may well have different ideas about what lock(-1) is for, and come to different conclusions. Whatever your decision, the solution should avoid the risk of deadlock IMHO.
- Peter
[ November 26, 2002: Message edited by: Peter den Haan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lock(-1)...