• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getRecord(int recNum) requires locking record first?

 
Richard Walter
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just would like to confirm that you do not need to lock the record if you just want to get a record from the database with no itention of modifying it.
My concern is that without locking the record, another client might be modifying the record while Im reading it, therefore I might get some erroneous data in my record.
I presume this will not happen as the getRecord and modifyRecord are both synchronised in the Data clas, thus preventing a read while a record is being written.
In my current implmentation I lock the record before reading it. If a connection attempts to read a record without obtaining a lock first an exception is thrown. I think this is all uncesssary.
am I right in my thinking or am I missing something?
Thanks,
Rich
 
mike seluker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need not have to lock before reading if u r not going to modify it....
That's how i've done it... in case of modification, you should obtain the lock for reading and subsequently modifying...
how u have implemented the -1(DB level) lock?? i'm having a hard time implementing it...
Originally posted by Richard Walter:
Just would like to confirm that you do not need to lock the record if you just want to get a record from the database with no itention of modifying it.
My concern is that without locking the record, another client might be modifying the record while Im reading it, therefore I might get some erroneous data in my record.
I presume this will not happen as the getRecord and modifyRecord are both synchronised in the Data clas, thus preventing a read while a record is being written.
In my current implmentation I lock the record before reading it. If a connection attempts to read a record without obtaining a lock first an exception is thrown. I think this is all uncesssary.
am I right in my thinking or am I missing something?
Thanks,
Rich

 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard, your thinking is clear and true
- Peter
[This message has been edited by Peter den Haan (edited December 06, 2001).]
 
Richard Walter
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Im glad my thinking is correct for once.
Mike,
This is how I implemented the database lock:
private synchronized void lockDatabase(Connection currentConnection) {
while (anotherConnectionOwnsDatabaseLock(currentConnection)
&& otherConnectionsOwnRecordLocks(currentConnection)) {
try {
wait();
} catch (InterruptedException e) {
//do nothing
}
}
databaseLockOwner = currentConnection;
}

Basically it checks if another client(connection) has the database lock as well as checking if any other connection has any records locked. It waits until neither a databse lock is held by another connection and no record locks are held by other connections.
Rich
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic