Date: 2004-07-17T16:05:01
   Editor: JasonStitt <[EMAIL PROTECTED]>
   Wiki: Cocoon Wiki
   Page: ConnectionPooling
   URL: http://wiki.apache.org/cocoon/ConnectionPooling

   Addressed getting connections in Java and Flow.

Change Log:

------------------------------------------------------------------------------
@@ -115,15 +115,64 @@
 ...
 }}}
 
+''The above code, or its expanded form in the Apache Cocoon docs, did not work 
for me. Specifically, importing {{{org.apache.cocoon.Roles}}} failed. It does 
not seem to exist anywhere in Cocoon. I have Cocoon 2.1.5.1. --JasonStitt, 
2004-07-17.''
+
+''I found this alternative, presented here in skeleton form:''
+
+{{{
+import java.sql.Connection;
+import java.sql.SQLException;
+
+// These are from the jar avalon-framework-api-VERSION.jar, in Cocoon's 
WEB-INF/lib
+import org.apache.avalon.framework.component.ComponentException;
+import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.ComponentSelector;
+import org.apache.avalon.framework.component.Composable;
+
+// This is from the jar excalibur-datasource-VERSION.jar in the same directory
+import org.apache.avalon.excalibur.datasource.DataSourceComponent;
+
+public class CLASSNAME implements Composable {
+    DataSourceComponent datasource = null;
+
+    public void compose(ComponentManager manager) throws ComponentException {
+           ComponentSelector dbselector =
+               (ComponentSelector) manager.lookup(DataSourceComponent.ROLE + 
"Selector");
+           datasource = (DataSourceComponent) dbselector.select("NameOfPool"); 
//name as defined in cocoon.xconf
+    }
+    
+    // You can then use datasource.getConnection() to get a 
java.sql.Connection object.
+}
+}}}
+
+Of course, it can also be a good idea to separate your data-access object from 
the Avalon framework. For example, create a go-between class that gets a 
connection from Cocoon's pool and exposes it as a generic Connection object. If 
your DAO does not depend on Cocoon, then you can use it easily in other 
contexts, whether those are other apps or unit tests.
+
 === Using the Pool in flowscript ===
 
-It is possible to use a defined pool in Flowscript. It is also possible to 
query a database in flowscript
-without setting up a pool. An example using both methods is in the 
samples/petstore directory. The
-file !PetStoreImpl.js will show you everything you wish to know.
+From Flowscript, you can instantiate a Java object that uses the connection 
pool, use the pool directly, or even query a database without using the pool. 
The petstore example, placed in {{{samples/blocks/petstore}}} in recent Cocoon 
versions, accesses the database in Flowscript. It demonstrates both using the 
pool and connecting to the database directly. 
+
+The bulk of the code handling database connections is in !PetStoreImpl.js. To 
see the definition of the Database object used, you need the source code to 
Cocoon. You can find the imported Database.js (and the files it calls) at:
 
-If you wish to see the source code for this database connection script, you 
should have the sources of
-cocoon. You can find the imported Database.js (and the files it calls) at:
 
{{{cocoon-2.1.4/src/blocks/petstore/java/org/apache/cocoon/components/flow/javascript}}}
 
+==== Using Java data-access objects in Flowscript ====
+
+This assumes that you have created a Java object that implements Composable, 
as described in the previous section.
+
+In Flowscript, you can use the cocoon.createObject() method to instantiate a 
class that implements an Avalon interface, such as Composable, but is not 
registered as a component. For example:
+
+{{{var dataObject = cocoon.createObject(Packages.package.DataObject);}}}
+
+Note that this doesn't seem to work if your constructor needs any parameters. 
But you also cannot just instantiate the class using {{{new}}}, because then 
{{{compose()}}} and any other functions implementing the Avalon component 
interfaces will not be called.
+
+==== Connecting to the database pool in Flowscript ====
+
+It's fairly simple:
+
+{{{
+var selector = 
cocoon.getComponent(Packages.org.apache.avalon.excalibur.datasource.DataSourceComponent.ROLE
 + "Selector");
+var datasource = datasourceSelector.select("NameOfPool");
+var connection = datasource.getConnection();
+}}}
 
-It is also wise to check out the mailing lists; you just may find your 
solution waiting for you:) 
+It is also wise to check out the mailing lists; you just may find your 
solution waiting for you :) 

Reply via email to