You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ZYD <el...@hotmail.com> on 2003/11/02 01:56:43 UTC

What's the best choice for connection pooling with Struts and PostgreSQL

Dear all,

I'm writing my Struts application using PostgreSQL as the backend database. Could you give me some advice on choosing the connection pooling framework/methods?

I wrote my own connection pooling classes, but if there are some good frameworks from jakarta, it definitely worth a  try.

Any response is greatly appreciated.

bruce

Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by ZYD <el...@hotmail.com>.
Thanks Nick.

What problem did you have with DBCP?

bruce
----- Original Message ----- 
From: "Nick Heudecker" <ni...@systemmobile.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Sunday, November 02, 2003 11:26 PM
Subject: Re: What's the best choice for connection pooling with Struts and PostgreSQL


> Hi Bruce,
> 
> I personally prefer C3P0 over DBCP.  I've had problems with DBCP in the past, and the support for C3P0 seems to be better.  You can find it on Sourceforge.
> 
> On Sun, Nov 02, 2003 at 08:56:43AM +0800, ZYD wrote:
> > Dear all,
> > 
> > I'm writing my Struts application using PostgreSQL as the backend database. Could you give me some advice on choosing the connection pooling framework/methods?
> > 
> > I wrote my own connection pooling classes, but if there are some good frameworks from jakarta, it definitely worth a  try.
> > 
> > Any response is greatly appreciated.
> > 
> > bruce
> -- 
> Nick Heudecker
> SystemMobile, Inc.
> Email: nick@systemmobile.com
> Web: http://www.systemmobile.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 

Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by Nick Heudecker <ni...@systemmobile.com>.
Hi Bruce,

I personally prefer C3P0 over DBCP.  I've had problems with DBCP in the past, and the support for C3P0 seems to be better.  You can find it on Sourceforge.

On Sun, Nov 02, 2003 at 08:56:43AM +0800, ZYD wrote:
> Dear all,
> 
> I'm writing my Struts application using PostgreSQL as the backend database. Could you give me some advice on choosing the connection pooling framework/methods?
> 
> I wrote my own connection pooling classes, but if there are some good frameworks from jakarta, it definitely worth a  try.
> 
> Any response is greatly appreciated.
> 
> bruce
-- 
Nick Heudecker
SystemMobile, Inc.
Email: nick@systemmobile.com
Web: http://www.systemmobile.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by Paul Thomas <pa...@tmsl.demon.co.uk>.
On 04/11/2003 12:51 ZYD wrote:
> Thanks Paul,
> 
> Could you please give me some code snippet about that factory parameter
> from the server.xml ?
> 
> Thanks again.
> 
> bruce


     <ResourceParams name="jdbc/mytestDB">
	<parameter>
		<name>factory</name>
		 
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
	</parameter>
	<parameter>
		<name>driverClassName</name>
		<value>org.postgresql.Driver</value>
	</parameter>
	.... 	....

HTH

-- 
Paul Thomas
+------------------------------+---------------------------------------------+
| Thomas Micro Systems Limited | Software Solutions for the Smaller 
Business |
| Computer Consultants         | 
http://www.thomas-micro-systems-ltd.co.uk   |
+------------------------------+---------------------------------------------+

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by ZYD <el...@hotmail.com>.
Thanks Paul,

Could you please give me some code snippet about that factory parameter from the server.xml ?

Thanks again.

bruce

> You're mising the factory parameter from your server.xml ResourceParams 
> section. BTW, you don't need that stuff in your struts config file if 
> you're using JNDI.


----- Original Message ----- 
From: "Paul Thomas" <pa...@tmsl.demon.co.uk>
To: "ZYD" <el...@hotmail.com>
Cc: "struts-user" <st...@jakarta.apache.org>
Sent: Wednesday, November 05, 2003 3:10 AM
Subject: Re: What's the best choice for connection pooling with Struts and PostgreSQL


> 
> On 04/11/2003 02:43 ZYD wrote:
> > I tried to use Tomcat's datasource pool via JNDI, but things not going
> > well.
> > 
> > I got the following exception when I tried to get connection:
> > 
> > org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of
> > class '
> > ' for connect URL 'null', cause:
> > java.lang.NullPointerException
> >         at java.util.StringTokenizer.<init>(StringTokenizer.java:146)
> >         at org.postgresql.Driver.parseURL(Driver.java:251)
> >         at org.postgresql.Driver.acceptsURL(Driver.java:159)
> >         at java.sql.DriverManager.getDriver(DriverManager.java:232)
> >         at 
> > org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSou
> > rce.java:743)
> >         at 
> > org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource
> > .java:518)
> >         at cpool.struts.action.LogonAction.testJNDI(LogonAction.java:185)
> >         at cpool.struts.action.LogonAction.execute(LogonAction.java:60)
> > ....................................................
> > ....................................................
> > 
> > In struts-config.xml, I added the following:
> > 
> >    <data-source>
> >      <set-property property="autoCommit" value="false"/>
> >      <set-property property="description" value="Data Source
> > Configuration"/>
> >      <set-property property="driverClass" value="org.postgresql.Driver"/>
> >      <set-property property="maxCount" value="4"/>
> >      <set-property property="minCount" value="2"/>
> >      <set-property property="password" value=""/>
> >      <set-property property="url" value="jdbc:postgresql:cpool"/>
> >      <set-property property="user" value="cpool"/>
> >    </data-source>
> > 
> > In server.xml:
> > 
> > <Resource name="jdbc/CPool" auth="Container"
> > type="javax.sql.DataSource"/>
> >   <ResourceParams name="jdbc/CPool">
> >   <parameter>
> >     <name>username</name>
> >     <value>cpool</value>
> >   </parameter>
> >   <parameter>
> >     <name>password</name>
> >     <value></value>
> >   </parameter>
> >   <parameter>
> >     <name>driverClassName</name>
> >     <value>org.postgresql.Driver</value>
> >   </parameter>
> >   <parameter>
> >     <name>url</name>
> >     <value>jdbc:postgresql:cpool</value>
> >   </parameter>
> >   <parameter>
> >     <name>maxActive</name>
> >     <value>8</value>
> >   </parameter>
> >   <parameter>
> >     <name>maxIdle</name>
> >     <value>4</value>
> >   </parameter>
> >  </ResourceParams>
> > 
> > Then, I wrote the following method in an Action to test the connection:
> > 
> > public void testJNDI()
> >  {
> >   try
> >   {
> >    Context initCtx = new InitialContext();
> >    Context envCtx = (Context)initCtx.lookup("java:comp/env");
> >    if (envCtx != null)
> >    {
> >     TTools.consolePrint("LogonAction", "envCtx", envCtx.toString());
> >    }
> >    //DataSource ds = (DataSource)envCtx.lookup("jdbc/CPool");
> >    DataSource ds =
> >     (DataSource)initCtx.lookup("java:comp/env/jdbc/CPool");
> > 
> >    Connection conn = ds.getConnection();
> >    if (conn != null)
> >    {
> >     TTools.consolePrint(conn, "Conn", conn.toString());
> >    }
> >    conn.close();
> >   }
> >   catch (NamingException e)
> >   {
> >    // TODO Auto-generated catch block
> >    e.printStackTrace();
> >   }
> >   catch (SQLException e)
> >   {
> >    // TODO Auto-generated catch block
> >    e.printStackTrace();
> >   }
> >  }
> > 
> > Does this problem sounds familiar to you ?
> > Please give some advice on this, thanks in advance.
> 
> You're mising the factory parameter from your server.xml ResourceParams 
> section. BTW, you don't need that stuff in your struts config file if 
> you're using JNDI.
> 
> -- 
> Paul Thomas
> +------------------------------+---------------------------------------------+
> | Thomas Micro Systems Limited | Software Solutions for the Smaller 
> Business |
> | Computer Consultants         | 
> http://www.thomas-micro-systems-ltd.co.uk   |
> +------------------------------+---------------------------------------------+
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 

Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by Paul Thomas <pa...@tmsl.demon.co.uk>.
On 04/11/2003 02:43 ZYD wrote:
> I tried to use Tomcat's datasource pool via JNDI, but things not going
> well.
> 
> I got the following exception when I tried to get connection:
> 
> org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of
> class '
> ' for connect URL 'null', cause:
> java.lang.NullPointerException
>         at java.util.StringTokenizer.<init>(StringTokenizer.java:146)
>         at org.postgresql.Driver.parseURL(Driver.java:251)
>         at org.postgresql.Driver.acceptsURL(Driver.java:159)
>         at java.sql.DriverManager.getDriver(DriverManager.java:232)
>         at 
> org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSou
> rce.java:743)
>         at 
> org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource
> .java:518)
>         at cpool.struts.action.LogonAction.testJNDI(LogonAction.java:185)
>         at cpool.struts.action.LogonAction.execute(LogonAction.java:60)
> ....................................................
> ....................................................
> 
> In struts-config.xml, I added the following:
> 
>    <data-source>
>      <set-property property="autoCommit" value="false"/>
>      <set-property property="description" value="Data Source
> Configuration"/>
>      <set-property property="driverClass" value="org.postgresql.Driver"/>
>      <set-property property="maxCount" value="4"/>
>      <set-property property="minCount" value="2"/>
>      <set-property property="password" value=""/>
>      <set-property property="url" value="jdbc:postgresql:cpool"/>
>      <set-property property="user" value="cpool"/>
>    </data-source>
> 
> In server.xml:
> 
> <Resource name="jdbc/CPool" auth="Container"
> type="javax.sql.DataSource"/>
>   <ResourceParams name="jdbc/CPool">
>   <parameter>
>     <name>username</name>
>     <value>cpool</value>
>   </parameter>
>   <parameter>
>     <name>password</name>
>     <value></value>
>   </parameter>
>   <parameter>
>     <name>driverClassName</name>
>     <value>org.postgresql.Driver</value>
>   </parameter>
>   <parameter>
>     <name>url</name>
>     <value>jdbc:postgresql:cpool</value>
>   </parameter>
>   <parameter>
>     <name>maxActive</name>
>     <value>8</value>
>   </parameter>
>   <parameter>
>     <name>maxIdle</name>
>     <value>4</value>
>   </parameter>
>  </ResourceParams>
> 
> Then, I wrote the following method in an Action to test the connection:
> 
> public void testJNDI()
>  {
>   try
>   {
>    Context initCtx = new InitialContext();
>    Context envCtx = (Context)initCtx.lookup("java:comp/env");
>    if (envCtx != null)
>    {
>     TTools.consolePrint("LogonAction", "envCtx", envCtx.toString());
>    }
>    //DataSource ds = (DataSource)envCtx.lookup("jdbc/CPool");
>    DataSource ds =
>     (DataSource)initCtx.lookup("java:comp/env/jdbc/CPool");
> 
>    Connection conn = ds.getConnection();
>    if (conn != null)
>    {
>     TTools.consolePrint(conn, "Conn", conn.toString());
>    }
>    conn.close();
>   }
>   catch (NamingException e)
>   {
>    // TODO Auto-generated catch block
>    e.printStackTrace();
>   }
>   catch (SQLException e)
>   {
>    // TODO Auto-generated catch block
>    e.printStackTrace();
>   }
>  }
> 
> Does this problem sounds familiar to you ?
> Please give some advice on this, thanks in advance.

You're mising the factory parameter from your server.xml ResourceParams 
section. BTW, you don't need that stuff in your struts config file if 
you're using JNDI.

-- 
Paul Thomas
+------------------------------+---------------------------------------------+
| Thomas Micro Systems Limited | Software Solutions for the Smaller 
Business |
| Computer Consultants         | 
http://www.thomas-micro-systems-ltd.co.uk   |
+------------------------------+---------------------------------------------+

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by ZYD <el...@hotmail.com>.
I tried to use Tomcat's datasource pool via JNDI, but things not going well.

I got the following exception when I tried to get connection:

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '
' for connect URL 'null', cause:
java.lang.NullPointerException
        at java.util.StringTokenizer.<init>(StringTokenizer.java:146)
        at org.postgresql.Driver.parseURL(Driver.java:251)
        at org.postgresql.Driver.acceptsURL(Driver.java:159)
        at java.sql.DriverManager.getDriver(DriverManager.java:232)
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSou
rce.java:743)
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource
.java:518)
        at cpool.struts.action.LogonAction.testJNDI(LogonAction.java:185)
        at cpool.struts.action.LogonAction.execute(LogonAction.java:60)
....................................................
....................................................

In struts-config.xml, I added the following:

   <data-source>
     <set-property property="autoCommit" value="false"/>
     <set-property property="description" value="Data Source Configuration"/>
     <set-property property="driverClass" value="org.postgresql.Driver"/>
     <set-property property="maxCount" value="4"/>
     <set-property property="minCount" value="2"/>
     <set-property property="password" value=""/>
     <set-property property="url" value="jdbc:postgresql:cpool"/>
     <set-property property="user" value="cpool"/>
   </data-source>

In server.xml:

<Resource name="jdbc/CPool" auth="Container"   type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/CPool">
  <parameter>
    <name>username</name>
    <value>cpool</value>
  </parameter>
  <parameter>
    <name>password</name>
    <value></value>
  </parameter>
  <parameter>
    <name>driverClassName</name>
    <value>org.postgresql.Driver</value>
  </parameter>
  <parameter>
    <name>url</name>
    <value>jdbc:postgresql:cpool</value>
  </parameter>
  <parameter>
    <name>maxActive</name>
    <value>8</value>
  </parameter>
  <parameter>
    <name>maxIdle</name>
    <value>4</value>
  </parameter>
 </ResourceParams>

Then, I wrote the following method in an Action to test the connection:

public void testJNDI()
 {
  try
  {
   Context initCtx = new InitialContext();
   Context envCtx = (Context)initCtx.lookup("java:comp/env");
   if (envCtx != null)
   {
    TTools.consolePrint("LogonAction", "envCtx", envCtx.toString());
   }
   //DataSource ds = (DataSource)envCtx.lookup("jdbc/CPool");
   DataSource ds =
    (DataSource)initCtx.lookup("java:comp/env/jdbc/CPool");

   Connection conn = ds.getConnection();
   if (conn != null)
   {
    TTools.consolePrint(conn, "Conn", conn.toString());
   }
   conn.close();
  }
  catch (NamingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  catch (SQLException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

Does this problem sounds familiar to you ? 
Please give some advice on this, thanks in advance.

bruce

----- Original Message ----- 
From: "Vic Cekvenich" <ce...@baseBeans.com>
To: <st...@jakarta.apache.org>
Sent: Monday, November 03, 2003 3:18 AM
Subject: Re: What's the best choice for connection pooling with Struts and PostgreSQL


> 1. It is a good practice to use the containers data source pool via 
> JNDI. Application servers like Tomcat, Resin, OrionServer, etc all 
> provide a "service" to the web app of a connection pool. You should not 
> code one, but use one.
> Ex:
>         Context ctx = new InitialContext();
>         DataSource ds = (DataSource) ctx.lookup("java:comp/env/mypool");
>         conn = ds.getConnection();
> 
> And you configure the pool in the container. Ex in Resin 3.03:
> <database>
>      <jndi-name>mypool</jndi-name>
>      <driver type="org.postgresql.jdbc3.Jdbc3ConnectionPool" >
> <user>bpuser</user>
>      <password>changeme</password>
>      <serverName>3.3.3.3</serverName>
> <databaseName>dbname</databaseName>
>      </driver>
>      <max-connections>3</max-connections>
> </database
> 
> The idea here is that you deploy same web app to staging or production, 
> without changing the app.
> 
> 2. You do not need to do #1 above either. Just like you use Struts and 
> not code to servlets, you should not code to JDBC. You should use a DAO, 
> such as iBatis.com (SQL based and my favorite) or Hibrenate.
> Ex in iBatis:
> List _resList;
> _sqlMap.startTransaction();
> _resList = _sqlMap.executeQueryForList("xmlNamedSQLquerry");
> 
> And you have a List of results. The JNDI, pool open and close it done 
> for you, you just work on your result list (that you can pass on to 
> display tag for example)
> 
> J2EE designed real nice and you should leverage it, not fight it.
> 
> hth,
> 
> 
> 
> ZYD wrote:
> > Dear all,
> > 
> > I'm writing my Struts application using PostgreSQL as the backend database. Could you give me some advice on choosing the connection pooling framework/methods?
> > 
> > I wrote my own connection pooling classes, but if there are some good frameworks from jakarta, it definitely worth a  try.
> > 
> > Any response is greatly appreciated.
> > 
> > bruce
> > 
> 
> -- 
> Victor Cekvenich,
> Struts Instructor
> (215) 321-9146
> 
> Advanced Struts Training
> <http://basebeans.com/do/cmsPg?content=TRAINING> Server Side Java
> training with Rich UI, mentoring, designs, samples and project recovery
> in North East.
> Simple best practice basic Portal, a Struts CMS, Membership, Forums,
> Shopping and Credit processing, <http://basicportal.com> software, ready
> to develop/customize; requires a db to run.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 

Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by Vic Cekvenich <ce...@baseBeans.com>.
1. It is a good practice to use the containers data source pool via 
JNDI. Application servers like Tomcat, Resin, OrionServer, etc all 
provide a "service" to the web app of a connection pool. You should not 
code one, but use one.
Ex:
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/mypool");
        conn = ds.getConnection();

And you configure the pool in the container. Ex in Resin 3.03:
<database>
     <jndi-name>mypool</jndi-name>
     <driver type="org.postgresql.jdbc3.Jdbc3ConnectionPool" >
		<user>bpuser</user>
     		<password>changeme</password>
     		<serverName>3.3.3.3</serverName>
		<databaseName>dbname</databaseName>
     </driver>
     <max-connections>3</max-connections>
</database

The idea here is that you deploy same web app to staging or production, 
without changing the app.

2. You do not need to do #1 above either. Just like you use Struts and 
not code to servlets, you should not code to JDBC. You should use a DAO, 
such as iBatis.com (SQL based and my favorite) or Hibrenate.
Ex in iBatis:
List _resList;
_sqlMap.startTransaction();
_resList = _sqlMap.executeQueryForList("xmlNamedSQLquerry");

And you have a List of results. The JNDI, pool open and close it done 
for you, you just work on your result list (that you can pass on to 
display tag for example)

J2EE designed real nice and you should leverage it, not fight it.

hth,



ZYD wrote:
> Dear all,
> 
> I'm writing my Struts application using PostgreSQL as the backend database. Could you give me some advice on choosing the connection pooling framework/methods?
> 
> I wrote my own connection pooling classes, but if there are some good frameworks from jakarta, it definitely worth a  try.
> 
> Any response is greatly appreciated.
> 
> bruce
> 

-- 
Victor Cekvenich,
Struts Instructor
(215) 321-9146

Advanced Struts Training
<http://basebeans.com/do/cmsPg?content=TRAINING> Server Side Java
training with Rich UI, mentoring, designs, samples and project recovery
in North East.
Simple best practice basic Portal, a Struts CMS, Membership, Forums,
Shopping and Credit processing, <http://basicportal.com> software, ready
to develop/customize; requires a db to run.



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: [OT] Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by ZYD <el...@hotmail.com>.
I found something at http://www.postgresql.org/docs/7.3/static/jdbc-datasource.html

Your suggestion is similar to what Struts In Action suggests, on page 125:

If your database management system provides its own DataSource that can be
used with Struts, you should consider using that implementation instead.


----- Original Message ----- 
From: "David Graham" <gr...@yahoo.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, November 03, 2003 3:10 AM
Subject: [OT] Re: What's the best choice for connection pooling with Struts and PostgreSQL


> 
> --- ZYD <el...@hotmail.com> wrote:
> > Dear all,
> > 
> > I'm writing my Struts application using PostgreSQL as the backend
> > database. Could you give me some advice on choosing the connection
> > pooling framework/methods?
> > 
> > I wrote my own connection pooling classes, but if there are some good
> > frameworks from jakarta, it definitely worth a  try.
> 
> Postgres provides its own implementation of the DataSource interface.  You
> can find it here:
> 
> http://jdbc.postgresql.org/
> 
> David
> 
> > 
> > Any response is greatly appreciated.
> > 
> > bruce
> 

[OT] Re: What's the best choice for connection pooling with Struts and PostgreSQL

Posted by David Graham <gr...@yahoo.com>.
--- ZYD <el...@hotmail.com> wrote:
> Dear all,
> 
> I'm writing my Struts application using PostgreSQL as the backend
> database. Could you give me some advice on choosing the connection
> pooling framework/methods?
> 
> I wrote my own connection pooling classes, but if there are some good
> frameworks from jakarta, it definitely worth a  try.

Postgres provides its own implementation of the DataSource interface.  You
can find it here:

http://jdbc.postgresql.org/

David

> 
> Any response is greatly appreciated.
> 
> bruce


__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org