The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question on search/update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question on search/update" Watch "Question on search/update" New topic

Question on search/update

archana kher

Joined: Oct 04, 2007
Posts: 16
I am working on urlybird assignment. From the decribed assignment there is no primary key to identify a particular record. (I am assuming a hotel can have more than one room to be rented.)

I have folowing test case:
1. User performs search.
2. fetches search results.
3. Recoed from result is updated (says room is set as booked).

Updated record should be reflected in the dbfile. As mentioned since I have no primary I have to ensure that appropiate record gets updated.

Here is what I do. Every record entry in the dbfile has a number assigned to it. This is a sequential number which also defines the record location in the db. This also helps me in jumping directly to a particular record location (to perform read/write).

A deleted record also gets a number. As deletion does not remove a record from the dbfile. Record is just marked as deleted.

I have kept this assigned number as primary key till the database session is alive. This number is not stored in the database file (as format of file cannot be modified)

1. Am I doing things rite? anybody has any suggestions??
2. In a concurrent user scenario, say 'A' & 'B' user performs search & yeild similar search results. I have taken care of concurrancy so there is no race condition. But say user A updates the record & save it. Now user B also perfoms some operation on same record as that of 'A'. Since search results are not locked, how should we handle this scenario? Actually 'A' has changed the state of a record & this is not reflected to 'B'. please let me know how should we handle this scenario??
Romeo Kienzler

Joined: Aug 15, 2007
Posts: 14
Hi Archana,

1. I think it is ablolutely ok to use the "position" of the record in the database file as a primary key since there is no other option. Don't forget to document it in choises.txt

2. Google for optimistic locking vs. pessimistic locking. You can achieve optimistic locking by the following update scenario:

- db.lock(recNo)
- (check if there is the record is already booked, if so, throw an exception)
- db.update(rechno,.....)
- db.unlock(recNo)

Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 360
I think, that it is not OK. Because recNo is an int and position is long. Unless you do some sort of conversion recno/(positionInFile/recordLength).
[ October 26, 2007: Message edited by: Pavel Kubal ]
Oguz Ozun

Joined: May 10, 2007
Posts: 19
Maybe, you can think about constraints instead of key fields. For example, you may assume that during creation of a new record, the same customer cannot make 2 reservations for the same day.
This time, you may also say in the assignment (at least in mine) there is such statement
... The system you're writing does not interact with these numbers (owner-customer id), rather it simply records them. ...

As you may guess, I am also in trouble about that topic
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
Hi Archana,

I’m using recNo as key as well.
RecnO is an int (in my assignment, might be different in others), and size is a long.
However: an int is large enough in this case.

About concurrency: in my application if a users wants to book a room (update a record), it reads the record again, to check if it is still available.


[ November 01, 2007: Message edited by: Herman Scheltinga ]
[ November 01, 2007: Message edited by: Herman Scheltinga ]
archana kher

Joined: Oct 04, 2007
Posts: 16
Thanks for the input guys.

Record nos I am talking about are valid only till the database session is alive. For me its a int.

There is difference between record nos & record location. I think I have confused few of you above.

A record is set of sequential bytes. Record length is fixed.

To a record which constitues of fixed length bytes I assign nos. Which depends of its postion in the db file.

This assigned nos is primary key for me. So 1st record is just end of header length.

so if I have to read 1st record I jump to that location using this formula.

header_length + ((record nos - 1) *size of each record).

No record nos are stored in the db.

Guys please let me know if I am successful in explaining you the concept. This is very imp as I need to document the same.
archana kher

Joined: Oct 04, 2007
Posts: 16
forgot to add, Herman thanks a ton, you solved my issues on search. I liked the idea!!! simple and sleek.
I agree. Here's the link:
subject: Question on search/update
It's not a secret anymore!