aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking the whole Data base if -1 see code???? 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 "Locking the whole Data base if -1 see code????" Watch "Locking the whole Data base if -1 see code????" New topic
Author

Locking the whole Data base if -1 see code????

Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
//contains locked records
private Vector r_lock = new Vector(); //Global declaration
public void lock(int record) throws IOException {
synchronized(r_lock)
{
while(r_lock.contains(new Integer(record)))
{
try
{
wait();
}
catch(InterruptedException e)
{
throw new IOException("lock failed");
}
}
r_lock.add(new Integer(record));
}

}
/**
* Unlock the requested record. Ignored if the caller does not have
* a current lock on the requested record.The record that was locked
* is removed from the Vector r_lock and all threads are notifyed.
*/
public void unlock(int record) {
synchronized(r_lock)
{
if(r_lock.contains(new Integer(record)))
{
r_lock.remove(new Integer(record));
}
r_lock.notifyAll();
}
}
Now My question is how to lock the whole database if -1??? and then how to unlock it
Thanks Lisa
Cleland Early
Ranch Hand

Joined: Apr 16, 2001
Posts: 38
How about modifying your lock code in this fashion:
while((r_lock.contains(new Integer(record))) | | (r_lock.contains(new Integer(-1))))
You shouldn't have to change your unlocking code.
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Thanks Alot if I do this as you mention above will it lock the whole Database???

Thanks Lisa
lokesh ks
Greenhorn

Joined: Apr 29, 2001
Posts: 5
Hi Lisa,
Vectors are by default synchronised objects and again your code synchronises on the vector object is it OK?
Is it not a overhead ??
Bye,
Lokesh
Cleland Early
Ranch Hand

Joined: Apr 16, 2001
Posts: 38
Adding the second condition to your while statement prevents any threads from continuing as long as r_lock contains -1, no matter which record they are trying to lock.
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Thanks Very much I see the logic now. If you never unlock -1 no other threads can get in essentially blocking until you reboot.
Thanks Lisa
stewchicken
Greenhorn

Joined: May 31, 2001
Posts: 4
Originally posted by Lisa Foster:
//contains locked records
private Vector r_lock = new Vector(); //Global declaration
public void lock(int record) throws IOException {
synchronized(r_lock)
{
while(r_lock.contains(new Integer(record)))
{
try
{
wait();
}
catch(InterruptedException e)
{
throw new IOException("lock failed");
}
}
r_lock.add(new Integer(record));
}

}
/**
* Unlock the requested record. Ignored if the caller does not have
* a current lock on the requested record.The record that was locked
* is removed from the Vector r_lock and all threads are notifyed.
*/
public void unlock(int record) {
synchronized(r_lock)
{
if(r_lock.contains(new Integer(record)))
{
r_lock.remove(new Integer(record));
}
r_lock.notifyAll();
}
}
Now My question is how to lock the whole database if -1??? and then how to unlock it
Thanks Lisa

i think should
use r_lock.wait()
and r_lock.notifyAll()
cos r_lock was synchorinized but not this.
Conor Allen
Ranch Hand

Joined: Apr 27, 2001
Posts: 32
Have you considered blockinh the thread that gets the 'database lock' until all the threads updating records have finished with their locks? - If you do not then the DB lock thread can potentially corrupt a record to which another thread thinks that it has exclusive access
Any thoughts?
Conor
Brian Besterman
Greenhorn

Joined: Jun 07, 2001
Posts: 2
I agree with Conor.
One solution may be to use a flag to indicate that a client wants a full lock, which prevents any new locks. Once all the current locks are released, the client can obtain the full lock.
Conor Allen
Ranch Hand

Joined: Apr 27, 2001
Posts: 32
OR ....
Once a full db lock is granted then block in the lock method until all outstanding locks have been released ....

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Locking the whole Data base if -1 see code????