X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 6E27E11F69CB for ; Thu, 27 Apr 2006 21:34:08 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 59300-09 for ; Thu, 27 Apr 2006 21:33:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 00D3111F66E6 for ; Thu, 27 Apr 2006 21:33:56 -0300 (ADT) Received: (qmail 4811 invoked from network); 28 Apr 2006 00:33:53 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.2?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 28 Apr 2006 00:33:53 -0000 In-Reply-To: <20060428000209.62614.qmail@web54206.mail.yahoo.com> References: <20060428000209.62614.qmail@web54206.mail.yahoo.com> Mime-Version: 1.0 (Apple Message framework v749.3) Content-Type: multipart/alternative; boundary=Apple-Mail-22-373506829 Message-Id: <2F891858-CC29-433D-B89F-56843A5127EF@fastcrypt.com> Cc: pgsql-jdbc@postgresql.org From: Dave Cramer Subject: Re: connection pooling with servlets Date: Thu, 27 Apr 2006 20:33:51 -0400 To: J. X-Mailer: Apple Mail (2.749.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200604/177 X-Sequence-Number: 15111 --Apple-Mail-22-373506829 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed 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 =20 > here. I'm trying to write a basic webstore app for a school =20 > project and I'm having trouble getting more than one servlet to =20 > access the database connection. I tried the method described on =20 > the PostgreSQL site - as I understood it. That entailed opening a =20 > connection in one servlet and then having code like this in other =20 > servlets that need connections: > > Connection grabConnection(HttpSession userSession){ > ds =3D (Jdbc3PoolingDataSource) userSession.getAttribute=20 > ("dSource"); > Connection con =3D null; > > try { > con =3D ds.getConnection(); > // use connection > } catch(SQLException e) { > // log error > } finally { > if(con !=3D null) { > try {con.close();}catch(SQLException e) {} > } > } > return con; > } > > The trouble is (I'm sure you see it already) that passing a =20 > dataSource object in a session doesn't seem to work. If I don't =20 > have a dataSource, then I can't make a connection and if I try to =20 > create a new connection, I get the error that is something like =20 > "Connection with that name already exists". Any help and I'd be =20 > gratefull. Thanks. > > Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. =20 > Great rates starting at 1=A2/min. --Apple-Mail-22-373506829 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 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.=A0 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.=A0 I tried the method described on the PostgreSQL site - as = I understood it.=A0 That entailed opening a connection in one servlet = and then having code like this in other servlets that need = connections:

=A0=A0=A0 Connection grabConnection(HttpSession = userSession){
=A0=A0=A0 =A0=A0=A0 ds =3D (Jdbc3PoolingDataSource) = userSession.getAttribute("dSource");
=A0=A0=A0 =A0=A0=A0 Connection = con =3D null;

=A0=A0=A0 =A0=A0=A0 try {
=A0=A0=A0 =A0=A0=A0 = =A0=A0=A0 con =3D ds.getConnection();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 = // use connection
=A0=A0=A0 =A0=A0=A0 } catch(SQLException e) = {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 // log error
=A0=A0=A0 =A0=A0=A0 } = finally {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if(con !=3D null) {
=A0=A0=A0= =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 try {con.close();}catch(SQLException e) = {}
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0= =A0=A0=A0 return con;
=A0=A0=A0 }

The trouble is (I'm sure = you see it already) that passing a dataSource object in a session = doesn't seem to work.=A0 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".=A0 = Any help and I'd be gratefull.=A0 Thanks.


Talk is cheap. = Use Yahoo! Messenger to make PC-to-Phone calls. Great rates starting at = 1=A2/min.

= --Apple-Mail-22-373506829--