So what is the tip from Randy?
I also have a possible suggestion, which may be of help:
If you are using JDBC, you should always ensure that you close the
connection to the database. Database connections typically consume
a lot of resource (Oracle uses a process per connection, I am not
sure about MySql or Postgres). Maybe it is the database connection
which is stealing all the memory? Your logic should look like:
try {
// Get connection
// Create statement
// Execute statement
}
catch( Exceptions here ) {
// Process exceptions
}
finally {
// Release the connection
}
Also, I make use of a connection pool to share database connections
among multiple modules within the application.
Match.
Gerd Trautner wrote:
>
> hi randy, thanks for your tips!
>
> i think i found the reason for the growing memory consumption of my
> servlets.
>
> preconditions:
> i use servlets to get data out and in a mysql database and generate
> html pages with PrintWriter.print(...) statements.
> my servlets often use:
> ...
> Statement Stmt = C.createStatement();
> ResultSet RS = Stmt.executeQuery("SELECT ..,..,.. from ..");
> while (RS.next())
> {
> //do something with data
> out.println(....);
> }
> ...
>
> on each request an instance of a servlet is created and the servlet
> starts reading data out of the database and writes it in the
> PrintWriter stream.
> But what happens if the PrintWriter has no target (because the user
> sends another request or more then one other requests)? I am not sure,
> but it seems that the servlet tries to write to the PrintWriter stream
> and has no success. the result is a growing amount of instances of a
> servlet which try to write to a PrintWriter and have no success. and
> it seems they keep try writing (and consuming memory) a long time ...
>
> my solution is to check the error state of a Printwriter before
> writing in it. so the code from above looks like this:
> ...
> Statement Stmt = C.createStatement();
> ResultSet RS = Stmt.executeQuery("SELECT ..,..,.. from ..");
> while (RS.next())
> {
> if (out.checkError()) break;
> //do something with data
> out.println(....);
> }
> ...
>
> for my application it seems to work, but i think there must be a better
> way,
> (why does the write statement throws an exception and the print
> statement not?)
> any ideas?
>
> Gerd
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]