RE: RE: Error in servlet

From: "chris markiewicz" <cmarkiew(at)commnav(dot)com>
To: "'Andreas Tille'" <tillea(at)rki(dot)de>
Cc: "'PostgreSQL General'" <pgsql-general(at)postgresql(dot)org>
Subject: RE: RE: Error in servlet
Date: 2000-09-15 11:21:39
Message-ID: 008f01c01f07$1e8293c0$dbb846c6@cmarkiewicz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)hub(dot)org [mailto:pgsql-general-owner(at)hub(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Tille 2000-09-15 11:36:05 RE: RE: Error in servlet
Previous Message Jerome Raupach 2000-09-15 09:15:16 Re: load data from a file