tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tom Drake" <...@pobox.com>
Subject Re: Error ajp13_process_callback - write failed
Date Tue, 06 Nov 2001 21:33:09 GMT
Chris:

You must close your files. Java does not guarantee that
files will be closed automatically. Garbage collection may
reclaim your dangling streams / file handles at some point.
But you cannot say when. You must always close files/urls/etc...
when you are done with them.

BTW, closing should be done in a finally block like this:

  public void foo (String fileName) throws java.io.IOException{
    java.io.BufferedReader in = null;

    try {
      in = new BufferedReader(new java.io.FileReader(fileName));
      for (String line=null; null != (line=in.readLine()); ) {
        // do something interesting ....
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }

Note, the chained Reader objects (e.g. BufferedReader and FileReader).
You must close the 'last' reader / stream that you create. Doing so causes
all other streams / readers in the chained to be closed as well.

Placing the close() in the finally block ensures that the resource is closed
whether or not an exception is thrown.

Of course, if you have lots of code that winds up looking like this, you
could use the TemplateMethod pattern to design a solution that allows you
write the resource management code only once and to concentrate on
writing application code. Something like this:

interface LineHandler {
  public void handleLine(String line);
}

public class MyReader {
...
  public void readFile(String fileName, LineHandler handler) throws
java.io.IOException {
    java.io.BufferedReader in = null;

    try {
      in = new BufferedReader(new java.io.FileReader(fileName));
      for (String line=null; null != (line=in.readLine()); ) {
        handler.handleLine(line);
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }
}
...

    myReader.readFile("someFileName", new LineHandler() {
        public void handleLine(String line) {
            // do something interesting with each line.
        }
    });

----- Original Message -----
From: "Chris Gokey" <cgokey@gcmd.gsfc.nasa.gov>
To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
Sent: Tuesday, November 06, 2001 12:47 PM
Subject: Re: Error ajp13_process_callback - write failed


| This time Tomcat crashed, I received some errors...  Is it possible to
| receive this kind of error in mod_jk.log if Tomcat can't open up any
| more files?
|
|  >>> [Mon Nov 05 06:41:41 2001]  [jk_ajp13_worker.c (381)]: Error
|  >>> ajp13_process_callback - write failed
|  >>> [Mon Nov 05 06:41:58 2001]  [jk_ajp13_worker.c (381)]: Error
|  >>> ajp13_process_callback - write failed
|  >>> [Mon Nov 05 07:19:23 2001]  [jk_ajp13_worker.c (381)]: Error
|  >>> ajp13_process_callback - write failed
|
| Tomcat would just hang and not serve any more requests.  I couldn't
| connect to it directly either via port 8080.
|
| Looking through the logs, this time I noticed I'm getting "too many
| files open" error in my application, so I carefully went through and
| found some instances where files where being opened but not closed.  How
| does Java work, if you exit a method will it automatically close the
| file, or does it wait for the garbage collector to do this?  I found
| some instances where some servlets didn't explicitly close the
| java.io.Writer from the HttpServletResponse class.
|
| I suppose maybe my application is eating up all the available file
| pointers in memory and tomcat just hung trying to open a file?  Is this
| a plausible theory?
|
| Chris
|
|
| Chris Gokey wrote:
|
| > I didn't try that.   So far it has been alive all day, so if it crashes
| > again, I'll let you know.  Any other logs that might help tell why it is
| >  crashing?  I don't see much in any of the other logs.  Is there a way
| > to increase the verbosity of the logs?
| >
| > Chris
| >
| > Larry Isaacs wrote:
| >
| >> This error occurs when mod_jk tries to write the response back
| >> to the browser.  I'm not sure if it is the Ajp13 equivalent
| >> of the "Connection reset by peer" exception when the user clicks
| >> the stop button.
| >>
| >> Does Tomcat still respond after the hang when accessed directly?
| >>
| >> Larry
| >>
| >>
| >>> -----Original Message-----
| >>> From: Chris Gokey [mailto:cgokey@gcmd.gsfc.nasa.gov]
| >>> Sent: Monday, November 05, 2001 6:55 PM
| >>> To: Tomcat Users List
| >>> Subject: Error ajp13_process_callback - write failed
| >>>
| >>>
| >>> Hi,
| >>>
| >>> I've installed Tomcat 3.3 and using mod_jk under Solaris 7.  Tomcat
| >>> hung on me twice recently and noticed this error in the mod_jk.log
| >>> before it crashed today.
| >>>
| >>> [Mon Nov 05 06:41:41 2001]  [jk_ajp13_worker.c (381)]: Error
| >>> ajp13_process_callback - write failed
| >>> [Mon Nov 05 06:41:58 2001]  [jk_ajp13_worker.c (381)]: Error
| >>> ajp13_process_callback - write failed
| >>> [Mon Nov 05 07:19:23 2001]  [jk_ajp13_worker.c (381)]: Error
| >>> ajp13_process_callback - write failed
| >>>
| >>> Does anyone know what would cause this error?  Also, I don't see
| >>> anything else in the other logs, for that matter that would give me a
| >>> clue.
| >>>
| >>> Thanks,
| >>> Chris
| >>>
| >>>
| >>> --
| >>> To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
| >>> For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
| >>> Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>
| >>>
| >>
| >> --
| >> To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
| >> For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
| >> Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>
| >>
| >>
| >
| >
| >
| >
|
|
|
|
| --
| To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
| For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
| Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>
|
|
|


--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message