• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile use

 
Jon Pengelly
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I use RandomAccessFile to work with the database file. I am split on two ways to use it:
1) Create a static RandomAccessFile variable. All methods that work with
the database file (create, update, delete in my implementation) will use this variable.
Advantages

-I am able to synchronize on this variable (instead of the method) thus allowing me to have no unnecessary logic stuck in a synchronized block.
Disadvantages
-I have the overhead of keeping this file open even when it is not being used.
-I have to worry about issues with closing this file when the server is shutdown or crashes.

2) Create the random access file within each method (create, update, delete). This will mean that I will have to synchronize the method in order to avoid more than one file altering operation going on at the same time.
Advantages and Disadvantages of this approach are the opposite of those for method 1.
What does everybody else think of this issue? In your opinion is there a clear choice or does it not really matter either way?
Thanks for all comments,
Jonathan
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

-I am able to synchronize on this variable (instead of the method) thus allowing me to have no unnecessary logic stuck in a synchronized block.

This is what I did. But my Data class was a singleton, so I didn't have a static raf. (Same principle though)
I like this approach better than the other one you mentioned.
I actually didn't do much in terms of closing the file other than calling raf.close() in my Data's finalize() method, which probably won't be called because it won't be garbage collected because the VM is shutting down. Might loose some points for that...
J
 
Don Wood
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacques,

I actually didn't do much in terms of closing the file other than calling raf.close() in my Data's finalize() method, which probably won't be called because it won't be garbage collected because the VM is shutting down. Might loose some points for that...

I thought that it is unsafe to close the file from a finalize method. Since it is not guaranteed that the finalize method will run, isn't it possible to lose data because buffers were not flushed?
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don,
Originally posted by Don Wood:

I thought that it is unsafe to close the file from a finalize method. Since it is not guaranteed that the finalize method will run, isn't it possible to lose data because buffers were not flushed?

I think you're correct about finalize. However, if you use the "rwd" mode as follows:

I think you're guaranteed that the buffers will always be flushed.
 
Don Wood
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George,
OK, I see how that helps but if I read the JavaDoc correctly, "rwd" writes the data but does not guarantee that the metadata is written. If the file grows, the length (which is a part of the metadata) needs to be written too, I would think. So it would seem that "rws" would be a better choice.
If I have that right then when would "rwd" be the right choice? When you know the data in the file will change but the file will not change in size?
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don,
Originally posted by Don Wood:
So it would seem that "rws" would be a better choice.
If I have that right then when would "rwd" be the right choice? When you know the data in the file will change but the file will not change in size?

You're absolutely right, "rws" is the correct choice. My mistake. I was trying to remember which one was for the data only and which one was for the data and the metadata and I screwed it up. If you were only doing updates then I guess "rwd" would be appropriate; if you can potentially change the file size then you want the "rws" mode.
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really messed up there then!
I didn't think about closing the file properly and I only used "rw".
Well. Not much I can do about it now.
J
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jacques,
Originally posted by Jacques Bosch:
I really messed up there then!
I didn't think about closing the file properly and I only used "rw".
Well. Not much I can do about it now.

Don't sweat the small stuff. It sounds like I did the same thing as you did (used "rw" mode, only closed the file in the finalize method) and it turned out OK.
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for helping me breathe easy again, George.
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey George. Turns out you were right.
I got full marks for my data store. Feeeeeeeuuuuuuuuuu....!!!
 
Don Wood
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacques,
Sorry to give you cause for concern. Glad it worked out ok.
Congratulations!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic