wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes indexing / primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "indexing / primary key" Watch "indexing / primary key" New topic
Author

indexing / primary key

Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
I finished all my back-end server code a while ago and have been working on the GUI. I started running through some scenarios in my head and got a little confused. In my B&S exam, the database provides no unique fields. I keep a hashmap that maps (int) record number to (long) position in the file. Each time a record is deleted or created, I call a method that jumps the begining of each record and inspects the first two bytes. If it's deleted, I ignore it (I actually add deleted positions to a queue and check the queue before I create a new record), if it's a valid record, I add the location and index to my map and increment my index counter.

So if I have the following values in my map:
0 70
1 238
2 459
3 690

And I delete record 2, record 3 now becomes record 2 and my map looks like this:
0 70
1 238
2 690

But what happens in the following scenario:

Thread A locks record 3.
Thread B locks record 2.
Thread B deletes record 2.
Thread A updates record 3.
...
but record 3 is actually record 2 as soon as thread B deletes the original record 2. So what happens to the update for record 3? My first thought is that I would have to lock access to the entire database, not just a single record. Or does this just mean that I should synchronize all my code on my index map?

Am I way off base on the way I'm handling "recNo"s ?


SCJP 5.0
Chih-Wei Lee
Ranch Hand

Joined: Feb 20, 2008
Posts: 129
Hi Joshua:
I have the similar primary key choice with you.
I also used the index as a primary key.
In my experience, when I deleted a record I just updated the flag to deleted and kept the other data (name, location) in database file.
If a new record is created, I will reuse this record number(index) and
update the new data to the same position.
Shift the record number (index) will have a little trouble like you metion.


SCEA, SCJD, SCDJWS, SCWCD, SCJP
 
jQuery in Action, 2nd edition
 
subject: indexing / primary key
 
Similar Threads
Help on this locking scenario
Bad locking solution !
question the lock() and unlock()
The mysterious 65/80 locking score
Records