Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Use record number as primary key? 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 "Use record number as primary key?" Watch "Use record number as primary key?" New topic

Use record number as primary key?

B Rowell

Joined: Mar 21, 2006
Posts: 1
I'm working on URLyBird 1.3.1 and trying to apply a design approach similar to that used in Denny's DVD project in the SJCD Exam with J2SE 5 book. The data files are similar except that the records in Denny's DVD project use UPC as the primary key and URLyBird has no explicit primary key but all of the required interface methods associated with records use record numbers as parameters or return values. In Denny's DVD project there is a recordNumbers Map containing the mapping beween a DVD's UPC and the record's file location, which is the resource used via read-write locks to control changes to the data file. I'm considering using the same approach where some simpler Collection object, such as a List, contain the file locations of each of the records and this Collection object controls changes to the file. I would use a Room value object which would have a record number attribute.

This approach seems natural; however, I'm concerned that binding the Room object too closely to the phyical location of the record in the file will make it more difficult to migrate to a real (non-file) database solution. Also, using this approach seems a little like overkill but I still need a way to lock the data file records while users are reading them and record number control seems central.

Can anyone see any real problems with the proposed approach?
Anything I need to be aware of?

Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
The data schema for ERLyBird db file doesn't contain a primary key so the only way to identify a record in the Database layer is via the record position.

I think it depends on your overall design, if you plan read and cache the records then you will need a list to store the records, however if you are just reading the records from the file each time, I don't think its necessary to store file locations of records as you can easily calculate the position if you store the offset of the first record i.e read the header and schema information and then store the file pointer.

Then because you now the length of each record you can seek to the corresponding record.

However you will still need a map for your locking manager.

I would recommend keeping the data layer generic, and just return the String array (Which could be compared to a ResultSet in JDBC).

Then you could create Data Transfer Objects in your business layer to return to your client.

Ed Tse
Ranch Hand

Joined: Sep 18, 2003
Posts: 183
RecNo is kind of treated as the primary key, however, a delete + create operation can mess up the primary key as it will be referring to a different record. Thus we need locking.
[ March 27, 2006: Message edited by: Ed Tse ]

SCJP, Pre-SCJD (URLyBird 1.3.1), Teradata Cert'd Prof
It is sorta covered in the JavaRanch Style Guide.
subject: Use record number as primary key?
It's not a secret anymore!