Hello 

I'm having the same problem, but I'm using the compose method directly 
in the bean and trying to get the connection in another method. 
So far I figured out that the datasource is not initialized, because 
I got a null pointer exception if I try to get the connection. 
I looked at the documentation at
http://xml.apache.org/cocoon/developing/datasources.html
how to initialize the database, but there is nothing like this. 
So can somebody tell how to initialize the datasource or what is wrong?

Another question concerning the documentation. In the given code snipet 
is an 

        import org.apache.cocoon.Roles;

Any time I tried this I got an error message while compiling and couldn't 
figure out where this class is and couldn't therefor use the 
  
         ComponentSelector selector = (ComponentSelector)
manager.lookup(Roles.DB_CONNECTION);

statement, because no Roles are defined. Is this an old and because
of this wrong documentation or did I just miss something?

My sample code is like this.


----- code -------------------------
public void compose(ComponentManager manager) throws ComponentException {
        try {
                ComponentSelector selector =
                        ( ComponentSelector ) manager.lookup(
DataSourceComponent.ROLE + "Selector" );
                        this.datasource = ( DataSourceComponent )
selector.select( "webapp" );    
                }
                catch ( ComponentException ce ) {
                        this.error = this.error + "compose Component
Exception: " + ce;
                }
                catch ( Exception e ) {
                        this.error = this.error + "compose Exception: " + e;

                }
        }
}

public void someMethod() {
        ...
        
        try {
                this.datasource.getConnection();
        }
        catch ( Exception e ) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                this.error = this.error + "GetAllUser Exception: " +
sw.toString();
        }
        
        ...
}
----- /code -------------------------


the stackTrace gives this:

----- stackTrace --------------------

Exception: java.lang.NullPointerException 

at test.UserBean.getAllUser(UserBean.java:443) 
at test.UserBean.<init>(UserBean.java:125) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAcces
sorImpl.java:39) 
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstruc
torAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:274) 
at java.lang.Class.newInstance0(Class.java:306) 
at java.lang.Class.newInstance(Class.java:259) 

at
org.apache.cocoon.acting.AbstractXMLFormAction.getFormModel(AbstractXMLFormA
ction.java:478) 
at
org.apache.cocoon.acting.AbstractXMLFormAction.getForm(AbstractXMLFormAction
.java:243) 
at test.invoiceUserEdit.prepare(invoiceUserEdit.java:71) 
at
org.apache.cocoon.acting.AbstractXMLFormAction.act(AbstractXMLFormAction.jav
a:277)
at
org.apache.cocoon.components.treeprocessor.sitemap.ActTypeNode.invoke(ActTyp
eNode.java:139) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:85) 
at
org.apache.cocoon.components.treeprocessor.sitemap.PreparableMatchNode.invok
e(PreparableMatchNode.java:166) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:109) 
at
org.apache.cocoon.components.treeprocessor.sitemap.PipelineNode.invoke(Pipel
ineNode.java:153) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:109) 
at
org.apache.cocoon.components.treeprocessor.sitemap.PipelinesNode.invoke(Pipe
linesNode.java:143) 
at
org.apache.cocoon.components.treeprocessor.TreeProcessor.process(TreeProcess
or.java:326) 
at
org.apache.cocoon.components.treeprocessor.TreeProcessor.process(TreeProcess
or.java:308) 
at
org.apache.cocoon.components.treeprocessor.sitemap.MountNode.invoke(MountNod
e.java:131) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:85) 
at
org.apache.cocoon.components.treeprocessor.sitemap.ActTypeNode.invoke(ActTyp
eNode.java:158) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:85) 
at
org.apache.cocoon.components.treeprocessor.sitemap.ActTypeNode.invoke(ActTyp
eNode.java:158) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:85) 
at
org.apache.cocoon.components.treeprocessor.sitemap.PreparableMatchNode.invok
e(PreparableMatchNode.java:166) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:109) 
at
org.apache.cocoon.components.treeprocessor.sitemap.PipelineNode.invoke(Pipel
ineNode.java:153) 
at
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invo
keNodes(AbstractParentProcessingNode.java:109) 
at
org.apache.cocoon.components.treeprocessor.sitemap.PipelinesNode.invoke(Pipe
linesNode.java:143) 
at
org.apache.cocoon.components.treeprocessor.TreeProcessor.process(TreeProcess
or.java:326) 
at
org.apache.cocoon.components.treeprocessor.TreeProcessor.process(TreeProcess
or.java:308) 
at org.apache.cocoon.Cocoon.process(Cocoon.java:595) at
org.apache.cocoon.servlet.CocoonServlet.service(CocoonServlet.java:1069) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) 
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:247) 
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:193) 
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:260) 
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
eNext(StandardPipeline.java:643) 
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480) 
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995) 
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:191) 
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
eNext(StandardPipeline.java:643) 
at org.apache.catalina.core.StandardPipeline.invoke(Standardpeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995) 
at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2396) 
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180
) 
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
eNext(StandardPipeline.java:643) 
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.
java:170) 
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
eNext(StandardPipeline.java:641) 
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172
) 
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
eNext(StandardPipeline.java:641) 
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480) 
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995) 
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:174) 
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
eNext(StandardPipeline.java:643) 
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480) 
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995) 
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223) 
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:405) 
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConne
ction(Http11Protocol.java:380) 
at
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:508) 
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.jav
a:533) 
at java.lang.Thread.run(Thread.java:536) 

----- /stackTrace -------------------


ciao Lars


> -----Ursprüngliche Nachricht-----
> Von: Bas Peters [mailto:[EMAIL PROTECTED]]
> Gesendet: Mittwoch, 18. Dezember 2002 06:52
> An: [EMAIL PROTECTED]
> Betreff: 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:     <[EMAIL PROTECTED]>
> For additional commands, e-mail:   <[EMAIL PROTECTED]>
> 

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

Reply via email to