Hi Patrick;

It seems to me that your idea (1) is an ok way to go.

As for db connections, your best bet maybe to initialise a db connecion pool in
the init(). Remember that the init() method is *not* executed every time the
doGet()/doPost is called. It is called once during initiailazation (more or
less: there was a long discussion about excatly when init() is executed a couple
or so weeks ago: check the archives if you want all the gory details  :-))). The
important thing to remember is that if you instead open a db connection in the
init() and there are several calls to your servlet then you may get confusing
results (unless you are careful to protect the relevant code in your db class)
because all the calls are then working with the same connection, which is
probably not your intention. Also performance will probably suffer if your site
is active.

One thing that you may want to watch out for while doing database processing is
this: close *every* statement.resultset that you create! Else your database will
end up with many open cursors and will eventually run into trouble. (it's hwat
causes the ORA-01000 error in case you are working against Oracle). So something
like this is a good idea:

Statement stmt = null; Resultset rs = null;
try{
    stmt = dbConn.createStatement();
    //create the querystring here..
    rs = stmt.executeQuery(queryString);
    //blah..
 }
catch(whatever){

..more blahs..
}
finally{
    try{
        rs.close();
        stmt.close;
    }
    catch(more..){
        etc.
    }
}

There's probably other things that people will come up with: but this last is
something we got nailed with last week and so is pretty fresh in my memory. <:->

Good luck!
Geeta

"Odowd, Patrick" wrote:

> I have a servlet that passes a customer id to a method in my database class,
> the database class returns the details for the customer id e.g. name,
> address etc. The database class is not a servlet.
>
> 1. Is this a good way to do this?
> 2. Where should I open the database connection - in the servlet's init() or
> in the database class itself?
> 3. What are the things I should look out for when doing database processing
> from servlets?
>
> Thanks.
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to