Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Some questions about database handling (B & S) 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 "Some questions about database handling (B & S)" Watch "Some questions about database handling (B & S)" New topic
Author

Some questions about database handling (B & S)

Andriy Tsykholyas
Greenhorn

Joined: Feb 24, 2009
Posts: 15
Hi all,

I have a few uncertainties about database in B & S:
1)

Assignment does not state when DuplicateKeyException should be thrown (in contrast to RecordNotFoundException). So, I'm simply don't throwing it. I suppose this is OK

2)

It is not states who can unlock the record. What if client A locks a record and client B tries to unlock it? I silently ignore such client B call.

3) The assignment claims that if text field value is shorter than maximum length for this field, it is "null terminated" (null character code is 0). In fact the unused part of the field is filled with spaces (code 32). So, I stick to spaces.
Maybe the problem is that I'm using RandomAccessFile to handle the DB file?

What are your thoughts about this?

thanks

Andriy
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

For create method, it depends on whether you reuse deleted records or not. Even if you don't the Data class seems hard to check if a record is "duplicated" hence you are correct, you don't have to throw DuplicateKeyException.

For the unlock method your thinking is also correct. And third query the null or spaces thing - spaces are fine.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Andriy Tsykholyas
Greenhorn

Joined: Feb 24, 2009
Posts: 15
K. Tsang wrote:For create method, it depends on whether you reuse deleted records or not. Even if you don't the Data class seems hard to check if a record is "duplicated" hence you are correct, you don't have to throw DuplicateKeyException.

For the unlock method your thinking is also correct. And third query the null or spaces thing - spaces are fine.


Thanks
Dmytro Stasyuk
Greenhorn

Joined: Apr 02, 2009
Posts: 5
Andriy Tsykholyas wrote:


Assignment does not state when DuplicateKeyException should be thrown (in contrast to RecordNotFoundException). So, I'm simply don't throwing it. I suppose this is OK
[code]



Perhaps you could assume that the combination of Name and Location fields forms a unique key in the database record entries. And in that case you could throw DuplicateKeyException if the user tries to add a new entry with name and location values which are equal to the values belonging to an already existing record entry.

I wonder if anybody has thought of doing this. I'm doing the SCJD assignment as well and am considering this option.

What do you think?
Andriy Tsykholyas
Greenhorn

Joined: Feb 24, 2009
Posts: 15
Dmytro Stasyuk wrote:
Andriy Tsykholyas wrote:


Assignment does not state when DuplicateKeyException should be thrown (in contrast to RecordNotFoundException). So, I'm simply don't throwing it. I suppose this is OK
[code]



Perhaps you could assume that the combination of Name and Location fields forms a unique key in the database record entries. And in that case you could throw DuplicateKeyException if the user tries to add a new entry with name and location values which are equal to the values belonging to an already existing record entry.

I wonder if anybody has thought of doing this. I'm doing the SCJD assignment as well and am considering this option.

What do you think?


I prefer to not make any limitations on database which are not required by the assignment. Reasons:
1) The simpler the code - the less errors in the code.
2) I suspect Sun would run some automated tests on the database class. And additional limitation on database may result in automatic failure.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Dmytro Stasyuk wrote:Perhaps you could assume that the combination of Name and Location fields forms a unique key in the database record entries. And in that case you could throw DuplicateKeyException if the user tries to add a new entry with name and location values which are equal to the values belonging to an already existing record entry.

I wonder if anybody has thought of doing this. I'm doing the SCJD assignment as well and am considering this option.

What do you think?


Hi Dmytro, your idea is a good idea but think about this: how do you read each field in the Data class? Unless you store each field immediately in a temp array or something, it's very hard to compare.

What I did in mine was I had a service layer and do the checking in the service layer. I did the URLyBird and had a Room class. I used the Room object's equals method to check this. So the Data class doesn't need to do this check.

Of course B&S is contractors and it depends on your architecture to some extend.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

My advice: if it's not a must in the assignment, don't add it in your code. so there are no requirements to check for a duplicate key, so don't throw the exception at all.

Allthough I have to say i'm doing the urlybird assignment and it's very reasonable to believe a hotel has several equal rooms (same name, location, size, rate,...) and you should not throw a DuplicateKeyException at all. Should be only ok if a room number (per hotel) is added to the database file for example.
But maybe in the B&S assignment there is a set of values that should be unique to make sense in reality and then you could introduce such a check and throw a DuplicateKeyException. Whatever you do certainly document it in your choices.txt

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Dmytro Stasyuk
Greenhorn

Joined: Apr 02, 2009
Posts: 5
K. Tsang wrote:Hi Dmytro, your idea is a good idea but think about this: how do you read each field in the Data class? Unless you store each field immediately in a temp array or something, it's very hard to compare.


I was thinking to just to iterate over all records, in a sense call the find by name and location method, before adding the new potentially duplicate record. Although it would not be very difficult to implement however if this is done for every new record it might slow down the application significantly when the db file growth large. But considering that creation of a new record would be a lot less frequent operation than say read it might be OK performance wise. But yes I do agree adding this extra complexity when it is not necessary is the easy way out .
Dmytro Stasyuk
Greenhorn

Joined: Apr 02, 2009
Posts: 5
Roel De Nijs wrote:But maybe in the B&S assignment there is a set of values that should be unique to make sense in reality and then you could introduce such a check and throw a DuplicateKeyException. Whatever you do certainly document it in your choices.txt


It does indeed look like the name and the location fields of the contractor could form a unique key. Some of the contractor entries contained in the original db file have the same name but different locations and vice versa, but never same name and location. So that could be the reason for doing it.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Dmytro Stasyuk wrote:Some of the contractor entries contained in the original db file have the same name but different locations and vice versa, but never same name and location.


Yes, currently, there isn't. But there could be two records with the same name and location. So it can't really be assumed to be a unique combination.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some questions about database handling (B & S)
 
Similar Threads
[URLyBirdHotel 1.3.3] Couldn't I use Data.java interface class?
URLyBird question
NX: URLyBird questions about database
Beta Question: unlock
URLyBird 1.2.1 - SecurityException