File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some questions about database handling (B & S)

 
Andriy Tsykholyas
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3227
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andriy Tsykholyas
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3227
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9190
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Dmytro Stasyuk
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic