Re: connection pooling with servlets

From: Dave Cramer <pg(at)fastcrypt(dot)com>
To: J(dot) <sweepingoar(at)yahoo(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: connection pooling with servlets
Date: 2006-04-28 00:33:51
Message-ID: 2F891858-CC29-433D-B89F-56843A5127EF@fastcrypt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Use a static method to get the connection instead of the session.

Dave
On 27-Apr-06, at 8:02 PM, J. wrote:

> I am a novice, but thought it would make more sense to post this
> here. I'm trying to write a basic webstore app for a school
> project and I'm having trouble getting more than one servlet to
> access the database connection. I tried the method described on
> the PostgreSQL site - as I understood it. That entailed opening a
> connection in one servlet and then having code like this in other
> servlets that need connections:
>
> Connection grabConnection(HttpSession userSession){
> ds = (Jdbc3PoolingDataSource) userSession.getAttribute
> ("dSource");
> Connection con = null;
>
> try {
> con = ds.getConnection();
> // use connection
> } catch(SQLException e) {
> // log error
> } finally {
> if(con != null) {
> try {con.close();}catch(SQLException e) {}
> }
> }
> return con;
> }
>
> The trouble is (I'm sure you see it already) that passing a
> dataSource object in a session doesn't seem to work. If I don't
> have a dataSource, then I can't make a connection and if I try to
> create a new connection, I get the error that is something like
> "Connection with that name already exists". Any help and I'd be
> gratefull. Thanks.
>
> Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.
> Great rates starting at 1¢/min.

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message J. 2006-04-28 00:59:15 Re: connection pooling with servlets
Previous Message J. 2006-04-28 00:02:09 connection pooling with servlets