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" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

Reply via email to