File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Lisa Foster
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alot if I do this as you mention above will it lock the whole Database???

Thanks Lisa
 
lokesh ks
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR ....
Once a full db lock is granted then block in the lock method until all outstanding locks have been released ....

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic