RE: RE: Error in servlet - Mailing list pgsql-general

From chris markiewicz
Subject RE: RE: Error in servlet
Date
Msg-id 008f01c01f07$1e8293c0$dbb846c6@cmarkiewicz
Whole thread Raw
In response to RE: Error in servlet  (Andreas Tille <tillea@rki.de>)
Responses RE: RE: Error in servlet  (Andreas Tille <tillea@rki.de>)
List pgsql-general
oh yeah, and you need to call rs.next()...

to you other point, yes and no.  for any regular class (non-static and
non-servlet), this is true.  the issue is this - there will only ever be one
copy of rs, con, and stmt.  imagine the following very realistic scenario:
one user hits the servlet and runs a query...then a second user calls the
servlet while the first is using the rs...the first user's data will be
shot.  he/she will either get nonsense data or a null ptr or something.  the
second user will probably get good data.  when writing my first servlet, i
chased down a similar problem for about a week (i didn't realize that it was
a problem until we started doing stress testing and people started getting
errors).

there are two obvious solutions to this problem...

1 (i DON'T recommend this one) declare the variables in a method and pass
them to other methods as necessary.

2 (i do recommend this one) many server side java programmers feel that
servlets should be a gateway and nothing else...essentially a servlet should
do little more than call your other classes.  i.e. put all of your db
processing in a separate class and call it from your servlet.

chris



-----Original Message-----
From: pgsql-general-owner@hub.org [mailto:pgsql-general-owner@hub.org]On
Behalf Of Andreas Tille
Sent: Friday, September 15, 2000 4:29 AM
To: chris markiewicz
Cc: 'PostgreSQL General'
Subject: [GENERAL] RE: Error in servlet


On Thu, 14 Sep 2000, chris markiewicz wrote:

> could this be a servlet/thread issue?  i cannot tell from the code
snippet,
Solved.  I really stupidly forgot an rs.next() :-(((.

> but remember that variables in a servlet with class scope are essentially
> static.  (i am guessing that query, rs, stmt, etc are all class scope.
this
> is very dangerous, in the programming sense of the word...)  i've had
> similar (but not the same) problems before.  as a general rule, i NEVER
put
> a class scope variable in a servlet unless i really mean to.
Well, that might be true for query and rs and I'll change that, but
in my opinion

public class ServletSQLClass
{
  private Connection          con;
  private Statement           stmt;

  ...
  con = DriverManager.getConnection(url,user,passwd);
  stmt = con.createStatement();
  ...
}

con and stmt have to be class scope to hold the connection to the
database and don't have to reopen over and over.  Or did I understand
something wrong?

Kind regards

         Andreas.


pgsql-general by date:

Previous
From: Jerome Raupach
Date:
Subject: Re: load data from a file
Next
From: Andreas Tille
Date:
Subject: RE: RE: Error in servlet