Access Keys:
Skip to content (Access Key - 0)

Knowledgebase


Configuring a Globus JNDI Datasource for a caGrid Service


Contents

Overview

Grid Services are deployed into the Globus web application, typically on a JBoss or Tomcat container. In this configuration there are 2 JNDI contexts: the Globus JNDI and the Container JNDI. Developers who are familiar with confguring JNDI datasources may be tempted to create the datasource in the container JNDI, but this will not work for Grid Services because they will not have visibility to this JNDI context.

This article is intended to provide the steps necessary to configure a C3P0-pooled datasource within the Globus JNDI context.

When configured as described here, your data source's JNDI name will follow this name structure.

java:comp/env/services/cagrid/[SERVICE_NAME]/[DATASOURCE-NAME]

NOTE: In this guide, $SERVICE_HOME will represent the location of your service's top-level directory.

Step 1 - Create the Datasource Resource

At the root level of each Introduce-generated Grid Service is the file jndi-config.xml. This file contains configuration information for the service.

  1. Open your services $SERVICE_HOME/jndi-config.xml in an editor.
  2. Find the service element for your main service.
    NOTE: The jndi-config.xml will have one service element for the main service and one for each service context. Make sure that you are adding the configuration to the main service entry.
  3. Within the service element, add the following additional resource:
        <resource name="[DATASOURCE-NAME]" type="com.mchange.v2.c3p0.ComboPooledDataSource">
          <resourceParams>
            <parameter>
              <name>factory</name>
              <value>org.apache.naming.factory.BeanFactory</value>
            </parameter>
            <parameter>
              <name>driverClass</name>
              <value>[DATABASE_DRIVER_CLASS]</value>
            </parameter>
            <parameter>
              <name>jdbcUrl</name>
              <value>[JDBCURL]</value>
            </parameter>
            <parameter>
              <name>user</name>
              <value>[USERNAME]</value>
            </parameter>
            <parameter>
              <name>password</name>
              <value>[PASSWORD]</value>
            </parameter>
            <!-- Pool Parameters -->
            <parameter>
              <name>maxPoolSize</name>
              <value>60</value>
            </parameter>
            <parameter>
              <name>acquireIncrement</name>
              <value>3</value>
            </parameter>
            <parameter>
              <name>acquireRetryAttempts</name>
              <value>10</value>
            </parameter>
            <parameter>
              <name>acquireRetryDelay</name>
              <value>1000</value>
            </parameter>
            <parameter>
              <name>initialPoolSize</name>
              <value>3</value>
            </parameter>
          </resourceParams>
        </resource>
    
  4. Update the JNDI resource to include specifics for your database.
    Configuration Item
    User Value
    [DATASOURCE-NAME] Your datasource name. Example: "myserviceds"
    [DATABASE_DRIVER_CLASS] The driver specific to your database. Example: com.mysql.jdbc.Driver
    [JDCBURL] The JDBC URL appropriate for your database.
  5. The code above contains several pool settings that may not be appropriate for your application. Please refer to the C3P0 configuration documentation for details.
  6. Save the file.

Step 2 - Add the C3P0 jar to the service

If your service is a caGrid Data Service that is backed by the Local caCORE SDK, the C3P0 jar may already be present in your services lib directory.
  1. Download the C3P0 0.9.1.2 zip archive.
  2. Extract the zip file.
  3. Copy the jar to your services lib directory.

Step 3 - Add Database specific jar(s) to the service's lib directory

We're assuming that you will use the JNDI pool name directly in code to obtain a connection. Add the connector jar that is appropriate to your database type and version.

Example: Mysql

Example Configuration for a Sample Grid Service Using MySQL and CSM tables

This sample configuration employs:

  • C3P0 datasource pool
  • MySQL 5.0.45 database containing CSM tables.

Datasource Information

Confguration Item Value
ServiceName SampleService
Datasource Name sampledbpool
Driver Class com.mysql.jdbc.Driver
Database Name sampledb
Host localhost
Port 3306
Username sampleuser
Password samplepass

Resource Added to jndi-config.xml

   <resource name="sampledbpool" type="com.mchange.v2.c3p0.ComboPooledDataSource">
      <resourceParams>
        <parameter>
          <name>factory</name>
          <value>org.apache.naming.factory.BeanFactory</value>
        </parameter>
        <parameter>
          <name>driverClass</name>
          <value>com.mysql.jdbc.Driver</value>
        </parameter>
        <parameter>
          <name>jdbcUrl</name>
          <value>jdbc:mysql://localhost:3306/sampledb</value>
        </parameter>
        <parameter>
          <name>user</name>
          <value>sampleuser</value>
        </parameter>
        <parameter>
          <name>password</name>
          <value>samplepass</value>
        </parameter>
        <!-- Pool Parameters -->
        <parameter>
          <name>maxPoolSize</name>
          <value>60</value>
        </parameter>
        <parameter>
          <name>acquireIncrement</name>
          <value>3</value>
        </parameter>
        <parameter>
          <name>acquireRetryAttempts</name>
          <value>10</value>
        </parameter>
        <parameter>
          <name>acquireRetryDelay</name>
          <value>1000</value>
        </parameter>
        <parameter>
          <name>initialPoolSize</name>
          <value>3</value>
        </parameter>
      </resourceParams>
    </resource>

Jars Added to Service's lib directory

The following jars were placed into the service's lib directory

  • c3p0-0.9.1.2.jar
  • mysql-connector-java-3.1.13-bin.jar

Code added to Obtain a Connection from the Pool

Now that you have your C3P0 pool configured, you can add some code into your Service operations to obtain a connection and execute queries. Placing this code directly into your services client will not work.

  1. Add imports to your operation's class.
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    
  2. Add code to your operation.
    
    try {
    // fetch a JNDI-bound DataSource
    InitialContext ictx = new InitialContext();
    DataSource ds = (DataSource) ictx.lookup( "java:comp/env/services/cagrid/\[SERVICE_NAME\]/\[DATASOURCE_NAME\]" );
    Connection conn = ds.getConnection();
    
    //Create and execute a query
    
    conn.close();
    } catch (NamingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    
    
Last edited by
Clayton Clark (774 days ago)
Adaptavist Theme Builder Powered by Atlassian Confluence