• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

implementing database lock

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to know if there can be another approach to implementing a database lock and some suggestions on my approach
private boolean lock=true;
public void lock(int record)throws IOException{
synchronized(v){
while((v.contains(new Integer(record)))| |(v.contains(new Integer(-1)))){
try{
v.wait();
}
catch(InterruptedException e){
throw new IOException("lock failed");
}
}
while(lock){
while((v.size()>0)&(record==-1)){
lock=false;
try{
v.wait();
}
catch(Exception b){}
}
v.add(new Integer(record));
break;
}
}
}
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand the need for the while(lock) block. You have already blocked while waiting for the specific record and for the -1. What else are you waiting on? Why not just do the v.add() call alone without the other while(lock) block.
Also, what is the advantage of synchronizing the v object and not the method. Just curious. (I have read somewhere this is dangerous)
[This message has been edited by Rick Fortier (edited June 14, 2001).]
 
amit sharma777
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rick, the idea there is that once a client calls a full db lock no other thread can lock after that & it should also wait for the records already inside the lock to finish
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
amit sharma777
Welcome to Javaranch. We follow some conventions pertaining to User names. The rules are specified at here . These conventions are strictly followed. There are no Exceptions in this case. In the simplest of terms names are required to have at least two words separated by a space. It would be advised that this be your real full name.
Your User name does not follow these conventions. It is recommended that you immediately register under a name conforming to the conventions.
I think i had told you this earlier. If you make additional posts by the same user name that you have now i will be forced to block your account.
Please change your User name.
Thanks.

[This message has been edited by Rahul Mahindrakar (edited June 15, 2001).]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
I understand why you're using the while(lock) to make sure that no client can obtain a record level lock while there is an ongoing FULL DATABASE LOCK. But from your implementation, clients who try to obtain locks after a full database lock, are returned (with empty hands?) Don't you think these clients should 'block' as well, and wait until the full database lock is released, and THEN try and obtain the locks they require?
The specifications say that there are timeouts in trying to obtain a lock so clients don't mind waiting for the locks to become available.
Lemme know what you think?
 
Rick Fortier
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit sharma777:
Hi Rick, the idea there is that once a client calls a full db lock no other thread can lock after that & it should also wait for the records already inside the lock to finish


Ok, I see what you are doing. I had to look at it twice. I think that someone else looking at your code might also misunderstand what is going on with while(lock) and this should be better commented or written like this:

Using that extra variable and then having to put a break in the end makes some really have to study your code to see what is going on.
 
Rick Fortier
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that Akanimo had the same trouble I did understanding it the way you coded it too. It looks like some clients would return "empty handed" with no lock.
So, what is the benefit of synchronizing the object and not the method?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have to synchronize the Boolean variable (lock) as well
before modifying it. I think you have it declared as boolean.
You need to make it a Boolean object since you can only synchronize on Object references.
Rick, as far as your question goes regarding synchronizing
the entire method, I think it will result in locking up the
current object reference (this). So all other synchronized methods will also block till this method gets executed.
So I think it is better to synchronize on just the required member variables rather than lock up the entire object.
Further it also helps in locking up the member variable
for just the required block of the method (critical section) rather than locking it for the entire method.
 
amit sharma777
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps the following looks better,also do we really need to use a Boolean lock variable and obtain a lock to this variable I don't think if there is a case of corruption in this case because once it's false no other thread can access it.

private boolean lock=true;
public void lock(int record)throws IOException{
synchronized(v){
while((v.contains(new Integer(record)))| |(v.contains(new Integer(-1)))| |(lock==false)){
try{
v.wait();
}
catch(InterruptedException e){
throw new IOException("lock failed");
}
}
while(lock){
while((v.size()>0)&(record==-1)){
lock=false;
try{
v.wait();
}
catch(Exception b){}
}
v.add(new Integer(record));
break;
}
}
}
anyways I was looking for somr other approach in this case i.e without a boolean flag or something else
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic