connection pooling with servlets

Lists: pgsql-jdbc
From: "J(dot)" <sweepingoar(at)yahoo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: connection pooling with servlets
Date: 2006-04-28 00:02:09
Message-ID: 20060428000209.62614.qmail@web54206.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

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&cent;/min.


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
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.


From: "J(dot)" <sweepingoar(at)yahoo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: connection pooling with servlets
Date: 2006-04-28 00:59:15
Message-ID: 20060428005915.94720.qmail@web54213.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Thanks for replying. I'm not sure if that would help, but maybe if I understand the suggestion better it will.

Right now I've got the index.jsp calling Login servlet via POST. Then Login creates the connection, puts it into a session with some other attributes and forward(req,res) to welcome.jsp. Welcome.jsp has a form that uses GET to call a Search servlet and this is where I get a null pointer on the connection object. I'm trying to get the session out of the request object, but it seems like the request loses state by the time I'm getting to it (in Search servlet).

Dave Cramer <pg(at)fastcrypt(dot)com> wrote: 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.


---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.


From: David Durham <ddurham(at)vailsys(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 05:28:51
Message-ID: 4451A813.6030403@vailsys.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

J. wrote:
> Thanks for replying. I'm not sure if that would help, but maybe if I
> understand the suggestion better it will.
>
> Right now I've got the index.jsp calling Login servlet via POST. Then
> Login creates the connection, puts it into a session with some other
> attributes and forward(req,res) to welcome.jsp.

Not much point in storing a datasource as a session attribute (1 datasource per application user?). More appropriate to make it application-wide by putting a datasource in a servlet context or, as someone else suggested, a static attribute/property/member/variable ... request.getSession().getServletContext().setAttribute(...). JNDI is another good place to put this sort of thing. An easy way to make sure your datasource is always available is to use a servlet context listener to create it when the application is initialized. Google for servlet context lifecycle should tell you how to do setup a listener.

Now, as for a null pointer on your connection object, maybe you have something misconfigured in your datasource. Username, password, host, and driver class are usual suspects.

> Welcome.jsp has a form
> that uses GET to call a Search servlet and this is where I get a null
> pointer on the connection object. I'm trying to get the session out of
> the request object, but it seems like the request loses state by the
> time I'm getting to it (in Search servlet).

Not sure. You can post your code and maybe get some help. Wait, is this homework?

-Dave


From: "J(dot)" <sweepingoar(at)yahoo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: connection pooling with servlets
Date: 2006-04-28 06:11:05
Message-ID: 20060428061105.37423.qmail@web54215.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Thanks. I posted some of the relevant code here;

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=016798

But I'm still baffled as to how to get access to a session in a servlet that was called by a jsp. Should servlets have access to sessions easily? Everyone is making it seem like this shouldn't be a problem, but then I've read in other places that a jsp calling a servlet clears whatever attributes were part of the request object.

David Durham <ddurham(at)vailsys(dot)com> wrote: J. wrote:
> Thanks for replying. I'm not sure if that would help, but maybe if I
> understand the suggestion better it will.
>
> Right now I've got the index.jsp calling Login servlet via POST. Then
> Login creates the connection, puts it into a session with some other
> attributes and forward(req,res) to welcome.jsp.

Not much point in storing a datasource as a session attribute (1 datasource per application user?). More appropriate to make it application-wide by putting a datasource in a servlet context or, as someone else suggested, a static attribute/property/member/variable ... request.getSession().getServletContext().setAttribute(...). JNDI is another good place to put this sort of thing. An easy way to make sure your datasource is always available is to use a servlet context listener to create it when the application is initialized. Google for servlet context lifecycle should tell you how to do setup a listener.

Now, as for a null pointer on your connection object, maybe you have something misconfigured in your datasource. Username, password, host, and driver class are usual suspects.

> Welcome.jsp has a form
> that uses GET to call a Search servlet and this is where I get a null
> pointer on the connection object. I'm trying to get the session out of
> the request object, but it seems like the request loses state by the
> time I'm getting to it (in Search servlet).

Not sure. You can post your code and maybe get some help. Wait, is this homework?

-Dave


---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.


From: David Durham <ddurham(at)vailsys(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 13:55:03
Message-ID: 44521EB7.8040604@vailsys.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

J. wrote:
> Thanks. I posted some of the relevant code here;
>
> http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=016798
>
> But I'm still baffled as to how to get access to a session in a servlet
> that was called by a jsp. Should servlets have access to sessions
> easily?

Just look at the servlet api that matches the specification in the deployment descriptor, which is a DTD or schema in WEB-INF/web.xml.


From: "J(dot)" <sweepingoar(at)yahoo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: changing my password under cygwin
Date: 2006-05-03 09:01:12
Message-ID: 20060503090112.36152.qmail@web54212.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

I've got postgresql installed with cygwin under xp. I need to alter my password but I can't find anything that works. Everything I've tried so far hasn't made the change and gives no feedback as to what might be wrong. Here is the command I use to run the psql tool:

psql -U username -d template1

Here are some things I've tried that didn't work:

alter user username with password 'userpass'

net user username userpass

This is the command I ran before I created my username, but now when I run it, it asks for a password which I either never knew or forgot:

psql -d template1

I could even delete the user and re-create it as long as I wouldn't lose access to the system. Thanks.


---------------------------------
Yahoo! Mail goes everywhere you do. Get it on your phone.


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: changing my password under cygwin
Date: 2006-05-03 11:36:55
Message-ID: 9F426917-67E6-408C-96B6-62E8AB764E16@fastcrypt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Please ask this on the correct list, this has nothing to do with JDBC

On 3-May-06, at 5:01 AM, J. wrote:

> I've got postgresql installed with cygwin under xp. I need to
> alter my password but I can't find anything that works. Everything
> I've tried so far hasn't made the change and gives no feedback as
> to what might be wrong. Here is the command I use to run the psql
> tool:
>
> psql -U username -d template1
>
> Here are some things I've tried that didn't work:
>
> alter user username with password 'userpass'
>
> net user username userpass
>
> This is the command I ran before I created my username, but now
> when I run it, it asks for a password which I either never knew or
> forgot:
>
> psql -d template1
>
> I could even delete the user and re-create it as long as I wouldn't
> lose access to the system. Thanks.
>
> Yahoo! Mail goes everywhere you do. Get it on your phone.