You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Bas Peters <ma...@bpeters.com> on 2002/12/17 23:52:12 UTC

return connection to jbean from connection pool question

I am trying to get familiar with Cocoon (2.1) and try to expand the XMLform
HowToWizard demo by adding an insert statement to write data to a MySQL
database. I added a jBean.insert() method in the HowToWizardAction class,
added an insert() method in the bean class and added a DB class with a
static method that returns a Connection object. This works.

To use a connection pool I added the driver in web.xml and the datasource in
cocoon.xconf
I tested the pool (demo) using a simple ESQL page. It works fine.

Then I changed the DB class to use a connection pool by implementing
Composable (code below), but I receive a null pointer exception from the
pipeline when insert is executed. When I put the code in the
HowToWizardAction class it works fine (since it's an action). I have the
feeling that the compose method is never executed. Do I have to add the DB
class in the sitemap (as what?) or the cocoon.xconf to make sure that DB is
initialized? Is this a useful approach in Cocoon, or should I use a
different approach to return a Connection from a connection pool to the bean
class?

The DB class using the connection pool:

<snippet>
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;

import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.excalibur.datasource.DataSourceComponent;

public class DB implements Composable {

    public DB() {}

    public static Connection getConnection() {
 Connection con = null;
 try {
     con = datasource.getConnection();
 } catch (SQLException e) {
     e.printStackTrace();
 }
 return con;
    }

    private static DataSourceComponent datasource;

    public void compose(ComponentManager manager) throws ComponentException
{
 ComponentSelector selector = (ComponentSelector)manager.lookup
     (DataSourceComponent.ROLE + "Selector");
 datasource = (DataSourceComponent)selector.select("demo");
    }
}
</snippet>


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>