Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sporadic error reading file

Status
Not open for further replies.

dognobbler

Programmer
Jul 4, 2003
46
GB
My site has a number of servlets that send emails out ( ie. when a user registers for an account). The text of the email is held in a text file which the servlets opens and reads using the following line:


FileReader fr4 = new FileReader("../webapps/uk2/jsp/contactMail1.txt");


This normally works fine however every couple of days when I attempt to access this servlet I get the following error

--------------------------------------------------------------------------------

type Exception report

message Internal Server Error

description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

exception

java.io.FileNotFoundException: ../webapps/uk2/jsp/contactMail1.txt (No such file or directory)

If I restart the server then everything reverts to normal and the servlet begins working fine again.

The problem appears to be related to the amount of traffic my site is getting. If my site is getting 800 impressions a day the error occurs approx every 3 to 4 days, if the site is receiving 400 impressions it occurs approx every 4-6 days.

Does anyone have any idea what the problem may be, and what I could do to fix it?

Regards


Andy
 
I believe this is concurrency issue. The excepption could have happened due to concurrently access to the file. I wrote a test with mutliple theads accessing the same file at the same time. And FileNotException does happened from time to time.

I would suggest you to have the method that accessing the file synchronized. Or have the file content cached due servlet init.

hope that helps.
 
Thanks for the suggestion.

But if it was a concurrency issue, why would it be resolved by restarting the server. Would there still be just as many people trying to access the file concurrently?

Andy
 
The file may have dead locked by the webserver due to concurrency issue. When you restart the server, it relese the file again. As you said, there still people try to access the file, that why the server keep getting FileNotFoundException after a while. The Exception only happen under certain condition, which take a while for that to happend. More people hitting the server, higher the change that happend.

By the way, in your code, has the file been close() after if has been read. In my test code, I first didn't close() file, when run in multiple theads, the Exception occur after a while. If I close() the file after finish reading the file. The Exception seem goes away.
 
Hi Byam, thanks for the suggestions.

Here is an example of my filereading code

fr2 = new FileReader("/home/popmate/webapps/uk2/jsp/inviteMailMember1.txt");

while( ( ch2 = fr2.read() ) != -1 )
{
sbContent.append( (char) ch2 );
}



I have not closed the file. Could you please tell me the code to do this.

Thanks.

Andy
 
Here you go:

fr2 = new FileReader("/home/popmate/webapps/uk2/jsp/inviteMailMember1.txt");

while( ( ch2 = fr2.read() ) != -1 )
{
sbContent.append( (char) ch2 );
}

// close the file here
fr2.close();
 
Even better, put the close() in 'finally', that way it ensure the file is closed even exception occur within the try block.

try {
fr2 = new FileReader("/home/popmate/webapps/uk2/jsp/inviteMailMember1.txt");

while( ( ch2 = fr2.read() ) != -1 )
{
sbContent.append( (char) ch2 );
}
} finally {
// close the file here
fr2.close();
}


 
Don't forget to flush the stream before closing :

fr2.flush();
fr2.close();
 
When I put fr2.flush();

I get the following error

inviteMailServlet3.java:128: cannot resolve symbol
symbol : method flush ()
location: class java.io.FileReader
fr2.flush();
^

Can you tell me what I have done wrong?

Andy
 
Do you have fr2 defined and set to null outside the try/catch block? something like:

FileReader fr2 = null;

Code:
try {
  fr2 = new FileReader ("/home/popmate/webapps/uk2/jsp/inviteMailMember1.txt");

} finally {
  if (fr2 != null) {
     fr2.flush();
     fr2.close();
  }
}
 
Sorry - I'm being an idiot - you can only flush an output stream/reader.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top