Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 9890 invoked from network); 18 Nov 2005 04:22:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 18 Nov 2005 04:22:32 -0000 Received: (qmail 19986 invoked by uid 500); 18 Nov 2005 04:17:56 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 18512 invoked by uid 500); 18 Nov 2005 04:17:07 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 18243 invoked by uid 99); 18 Nov 2005 04:15:40 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 17 Nov 2005 20:15:14 -0800 Received: (qmail 7289 invoked by uid 65534); 18 Nov 2005 04:14:54 -0000 Message-ID: <20051118041454.7286.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r345438 [5/286] - in /cocoon/site/site/2.1: ./ developing/ developing/portal/ developing/portal/coplets/ developing/webapps/ developing/webapps/authentication/ faq/ howto/ installing/ plan/ plan/documentation/ plan/otherplanning/ plan/overv... Date: Fri, 18 Nov 2005 04:13:22 -0000 To: cvs@cocoon.apache.org From: crossley@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: cocoon/site/site/2.1/developing/datasources.html URL: http://svn.apache.org/viewcvs/cocoon/site/site/2.1/developing/datasources.html?rev=345438&view=auto ============================================================================== --- cocoon/site/site/2.1/developing/datasources.html (added) +++ cocoon/site/site/2.1/developing/datasources.html Thu Nov 17 20:00:02 2005 @@ -0,0 +1,1400 @@ + + + + + + + +Using Databases in Apache Cocoon + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+

Using Databases in Apache Cocoon

+ +

How do I choose my database?

+
+

Apache Cocoon is flexible in the way it allows you to make connections to a +database. There are basically two ways: by redefining all the connection +parameters in each page you use a database, or using a pooled connection. The +first method is slow and doesn't scale well. The second method is more scalable, +and depending on your database will realize true improvements.

+ +

Installing the Driver

+

Independent of how you choose to get and maintain your JDBC connections, you +have to load the driver so Cocoon can use it (unless you are using a J2EE +container--more on that later). This is an init parameter in your web.xml file. +The following snippet will show you how:

+
+      
+<init-param>
+  <param-name>load-class</param-name>
+  <param-value>
+    <!-- For PostgeSQL Database: -->
+    postgresql.Driver
+
+    <!-- For Oracle Database: -->
+    oracle.jdbc.driver.OracleDriver
+  </param-value>
+</init-param>
+      
+     
+

You can place as many Driver classes in this parameter you want. They are +separated by white space or commas.

+ +

Defining a Data Source

+

Cocoon allows you to specify a pooled data source that you can use for +throughout the Cocoon system. There are two different types of data sources: +JDBC and J2EE. The difference is in who controls the connection. The JDBC data +source lets Cocoon handle all the pooling logic. The J2EE data source tells +Cocoon how to pull the DataSource object from a J2EE container (thats Java 2 +Enterprise Edition)--the major caveat is that Cocoon must be installed as part +of a Enterprise Application.

+

The following snippet of cocoon.xconf shows the section where the +DataSourceComponent is specified. You can have more than one in this location. +The code will have one connection for the JDBC data source, and one connection +for the J2EE data source.

+
+      
+  <datasources>
+    <jdbc name="MyConnectionName">
+
+      <pool-controller min="5" max="10"/>
+      <dburl>jdbc:oracle:thin:@localhost:1521:mydatabase</dburl>
+      <user>mylogin</user>
+      <password>myPassword</password>
+    </jdbc>
+    <j2ee name="MyJ2eeConnection">
+      <dbname>cocoonDB</dbname>
+    </j2ee>
+  </datasources>
+      
+     
+ +

The JDBC Connection Properties

+

The JDBC connection has up to five different properties--but only one is +absolutely required.

+
    + +
  • dburl: This is absolutely required. Without it JDBC can't connect to the +database.
  • + +
  • user: This is only required if the database admin requires you to log in to +the database.
  • + +
  • password: This is only required if the database admin requires a password to +connect to the database.
  • + +
  • pool-controller: This has two parameters with defaults. If it is not +specified, the defaults are used. +
      + +
    • min: The minimum number of connections the pool will keep available at one +time. Defaults to zero (0).
    • + +
    • max: The maximum number of connections the pool will have created at the +same time. Defaults to three (3).
    • + +
    • oradb: If you have an Oracle database, you should add the attribute "oradb" +and set it to true.
    • + +
    + +
  • + +
  • auto-commit: If you need to ensure an autocommit is set to true or false, +then create the "auto-commit" element.
  • + +
+ +

The J2EE Connection Property

+

The J2EE connection has only one property and it is absolutely required. +Cocoon uses JNDI to look up the DataSource with the name you specified in +"dbname".

+ +

Using the Data Source Component

+

No matter how you defined your DataSourceComponent, you access it the same +way. Because The DataSourceComponent is a Component, your class needs to +implement the Avalon Composable interface. The Avalon Framework will give your +class a ComponentManager. At that point, it is up to you how and when you pull +the DataSourceComponent out of the ComponentManager.

+
+       
+import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.ComponentSelector;
+import org.apache.cocoon.Roles;
+import org.apache.avalon.excalibur.datasource.DataSourceComponent;
+
+import java.sql.Connection;
+
+// .... Skip a lot of lines until we are in the method you use
+//      to initialize the DataSourceComponent ....
+
+private DataSourceComponent datasource;
+
+public void compose(ComponentManager manager) {
+    ComponentSelector selector =
+        (ComponentSelector) manager.lookup(Roles.DB_CONNECTION);
+    this.datasource = (DataSourceComponent) selector.select("MyConnectionName");
+}
+
+// .... Skip more lines until we actually need to use the datasource
+
+private void meMethod() {
+    Connection myConnection = this.datasource.getConnection();
+
+    // .... perform SQL code here
+
+    myConnection.close();
+}
+       
+      
+

Notice that once you obtained your connection, you did nothing out of the +ordinary to return the connection to the pool? This is by design, and a result +of the JDBC specification. Basically the JDBC specification states that if a +driver implements pooled connections, then it should not alter the way those +connections are used. This maintains the portability of your code.

+ +
+
+ +
 
+
+ + + Propchange: cocoon/site/site/2.1/developing/datasources.html ------------------------------------------------------------------------------ svn:eol-style = native