• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 116
  • 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
 
Ranch Hand
Posts: 38
  • 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
  • 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
 
Greenhorn
Posts: 5
  • 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
  • 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
  • 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
 
Greenhorn
Posts: 4
  • 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.
 
Ranch Hand
Posts: 32
  • 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
 
Greenhorn
Posts: 2
  • 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
  • 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 ....

 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic