aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RandomAccessFile do not write! 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 "RandomAccessFile do not write!" Watch "RandomAccessFile do not write!" New topic
Author

RandomAccessFile do not write!

Luiz Reginaldo Curado
Ranch Hand

Joined: Jan 19, 2006
Posts: 108
Hi all, I really need some help in this question.

I'm testing my lock schema, creating several threads to delete one data record. My problem is... the first thread write the deleted flag to file, but the second thread read this flag as "not deleted". The acess to the file is synchonized.

In other words... The "write" of the first thread fails! Is JVM doing a cache of the file in memory?

Please, help me with this challenge...
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Hi Luiz,

It's a bit difficult to help you without seeing the code, what mode are you using for the RandomAccessFile contructor and how are you writing the delete flag? Is the move to the correct position in the file and also the write synchronized?

If can post a small segment of your write method may be we can help further.

regards
Jason
Luiz Reginaldo Curado
Ranch Hand

Joined: Jan 19, 2006
Posts: 108
file = new RandomAccessFile("mydb.db", "rws") ;

...

synchronized(file) {

file.seek( recordPosition ) ;

// Write flag data
byte flag = XXXX
file.writeByte( flag ) ;
}
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Your code for writing the invalid flag looks okay, does the record eventually get marked for deletion by any of the threads? as I'm wondering if there is a problem with your locking mechanism, i.e, could more than one thread read the flag before it's been marked for deletion?

Jason
[ September 01, 2006: Message edited by: Jason Moors ]
Bod Toki
Ranch Hand

Joined: Sep 01, 2006
Posts: 95
hiya, Luiz...

...i'm assuming your locking is alright and i'm also taking a guess that the problem may be with your file pointer position- the two threads may be accessing different positions in your file. try writing (and reading) characters to the file... that way you can open the file in a text editor to see what's going on- you may even manually edit the file yourself to see what happens.
...some other thing some of the bigger boys do (which could make your code a little fuller) is to do some logging (see: ).

...hope this helps.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please treat the following a a little brainstorm before the first cup of strong coffee this morning.

Ok, you have done file.writeByte(flag). Does that write have to be done immediately? Like right now? How can you ensure that it is?

(I might be a little hasty on that one, I'll just check the API retrospectively)

Yep I was :roll: The "s" in your mode should ensure the file gets flushed. There would be no harm in trying a file.flush() just in case the OS is not honouring the implicit flush. But there is no flush() method on RandomAccessFile.

So it's going to be something else.

What OS are your using? No, that's not going anywhere.

Is there just one instance of that RandomAccessFile object (file) you are synchronizing the threads on?

In my assignment the delete flag is two bytes, yours is just one byte?
[ September 02, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I just wrote a test program starting 500 reader threads synching on one RandomAccessFile instance. As soon as the main thread (also synched on the single file object) changed the value in the file, the reader threads noticed it. That was on UBUNTU linux.
Luiz Reginaldo Curado
Ranch Hand

Joined: Jan 19, 2006
Posts: 108
Hi all!

Thanks for your replys! My error was an exception in another place of the code in wich I had not put an printStackTrace...

But I found some real bugs in my locking schema, and thanks to you I have correted it.

Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RandomAccessFile do not write!