Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S => db error handling

 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm almost finished and I'm into intensive testing...

I ran into an obvious case when I'm trying to produce an IO error on the database. While the client is running, I rename the database (I use cached records, so the file is only in use when writing).

Then when writing, RandomAccessFile used with "rw" will create the file if it does not exist, and it seems no options are available to prevent the file from being created... Another mode "writing but do not create" would fix that for me...

Are you guys handling this scenario ? How could I prevent writing into a "new" file only allow writing into an existing file ? Currently I do a "exist" check on the "File" object, but few milliseconds later when the databaseFile is removed, the same scenario is present.

Your input will be appreciated
Alex
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok well.. I improved my code this way for now:



Not super nice to look at it, but does the job. THe first open in read mode reserves the file and checks if the file exist. If the exception is not thrown, then I open it in rw mode.

any other idea (beside maybe that it's beyond the assignment ) ?

Regards,
Alex
 
Romeo Son
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

code:

try{
fileAccess = new RandomAccessFile(file, "r");
fileAccess = new RandomAccessFile(file, "rw");
}catch(FileNotFoundException e){
//throw what you want..
}


I assume file is java.io.File. Then you can do:

if (!file.isFile()) {
throw new IllegalArgumentException(file + " is not a valid file");
}
fileAccess = new RandomAccessFile(file, "rw");
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But as I was saying, it does not prevent 1 millisecond after the check that the file is deleted, and you end up writing into a newly created empty file.


By opening in read mode, you ensure that the file cannot be modified on the file system, and then opening in read-write.

You see the point I'm making ? What do you think ?
 
uzma ali
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I read you post I worked on the code and came up with this
It's not a fancy one but it does help and one can improve upon it


Please comment
 
archana kher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No matter you are using cached records, but ideally the database is in use, so no external application should tamper the in operation file. I feel you need to look into this logic!! Keep the file opened don't let anyone else tamper the file. Use caching which is really good.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I fixed this by:
- opening in read mode (which will prevent the file form being altered)
- opening in read-write
- reading the MAGIC_NUMBER to make sure I'm writing in the correct file.

I do this because I don't keep the file open all the time, only to write/update the records.

Everything is in order now, thanks for your input!

Alex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic