Re: Best Practice to Create a Connection Pool in PostgreSQL

From: "Greg James" <gregcjames(at)comcast(dot)net>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Best Practice to Create a Connection Pool in PostgreSQL
Date: 2008-09-17 02:47:35
Message-ID: 4CCDCC902ACE4F27A8A526B67C510535@Serotonin
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Thanks for the advice.

Because I'm using Apache Tomcat 6, I ended up using the built-in Jakarta
Commons DBCP to create a connection pool to Postgres. Not sure if this is a
best practice or not, but it appears to be working well.

For those interested in doing this on Tomcat, but don't know where to start,
my setup is below.

Thanks to those who provided the examples I found on the web and the text
"Professional Apache Tomcat 6" from Wrox.

I created a file "context.xml" in my web app's META-INF directory with the
following information:

<Context path="/your_web_app_name">
<Resource name="jdbc/YourPool"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="20000"
removeAbandoned="true"
removeAbandonedTimeout="120"
username="username"
password="password"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://your_server_ip:5432/your_postgres_database_name"
/>
</Context>

From a java class you can access the connection pool by using the following
imports...

import java.sql.*;
import javax.sql.*;
import javax.naming.*;

...and java code wrapped in a try/catch block...

String dsString = "java:/comp/env/jdbc/YourPool";
DataSource ds = (DataSource) new InitialContext().lookup(dsString);
Connection con = ds.getConnection();

-----Original Message-----
From: pgsql-jdbc-owner(at)postgresql(dot)org
[mailto:pgsql-jdbc-owner(at)postgresql(dot)org] On Behalf Of John R Pierce
Sent: Tuesday, September 16, 2008 7:58 PM
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [JDBC] Best Practice to Create a Connection Pool in PostgreSQL

Greg James wrote:
>
> Hi,
>
> I've been looking through the mail archives in the JDBC forum and have
> seen a number of different ways to create a JDBC connection pool to
> Postgres. Could someone point me in the right direction, eg
> Jdbc3ConnectionPool, Apache's DBCP, etc.?
>

One reason there are so many ways is that there are different
requirements for connection pools.

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Guillaume Cottenceau 2008-09-17 08:29:28 Re: Informacion del JDBC
Previous Message John R Pierce 2008-09-17 01:57:37 Re: Best Practice to Create a Connection Pool in PostgreSQL