6.4. Manual Configuration and Deployment

Under the covers, the console creates a Geronimo deployment plan for the database pool, and then deploys the appropriate RAR file with that plan using the same deployment logic that the command-line deployer uses. After deploying a database pool, if you look at the list of deployed RAR files, you'll see an entry for the database pool.

[Note]Note

The name (or configId) of the deployed RAR file is not important except to stop or undeploy it. When application modules want to refer to the database pool, they'll use the name configured in the database pool screen in the console, or the name specified in the connectiondefinition-instance if the plan is deployed manually.

In other words, the console is not doing anything special, and a database pool can be configured and deployed manually. This may be especially useful if you want to deploy the same pool to multiple servers, or save the configuration for every developer to apply to their local instance later. This section describes how to configure and deploy database pools using the underlying text files and command-line tools.

6.4.1. Configuring a Database Pool

No matter how you plan to deploy the database pool, the basic configuration format is the same. The only difference is that for server-wide and application-scoped database pools, the configuration goes in a standalone file, while for client-scoped database pools, the same configuration information is embedded in the module's Geronimo configuration file.

[Note]Note

Geronimo does not yet handle drivers that supply their own DataSource or ConnectionPoolDataSource; instead Geronimo uses the Driver class or XADataSource class supplied by the JDBC driver.

Since the pool is deployed as a connector, the configuration file is a connector deployment plan, using the same format covered in Chapter 13, J2EE Connectors (RARs) [DRAFT (1.0)]. For now, we'll look at the key elements of this file as far as database pools are concerned. Example 6.1, “Database Pool Deployment Plan” shows a sample database pool deployment plan. Note that the configuration information shown there may be saved to a file on its own or embedded in another file depending on how you deploy the database pool -- see Section 6.4.2, “Deploying a Database Pool” for more details.

Example 6.1. Database Pool Deployment Plan

<connector
    xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.0"
    version="1.5"
    configId="MyDatabase"
    parentId="geronimo/j2ee-server/1.0/car>

  <dependency>
    <uri>postgresql/pg74/213.jdbc3/jar</uri>
  </dependency>

  <resourceadapter>
    <outbound-resourceadapter>
      <connection-definition>
        <connectionfactory-interface>
          javax.sql.DataSource
        </connectionfactory-interface>
        <connectiondefinition-instance>
          <name>PostgreSQLDataSource</name>
          <config-property-setting name="UserName">
            dbuser
          </config-property-setting>
          <config-property-setting name="Password">
            dbpw
          </config-property-setting>
          <config-property-setting name="Driver">
            org.postgresql.Driver
          </config-property-setting>
          <config-property-setting name="ConnectionURL">
            jdbc:postgresql://localhost/mydb
          </config-property-setting>
          <config-property-setting name="CommitBeforeAutocommit">
            true
          </config-property-setting>
          <config-property-setting name="ExceptionSorterClass">
            org.tranql.connector.NoExceptionsAreFatalSorter
          </config-property-setting>
          <connectionmanager>
            <local-transaction/>
            <single-pool>
              <max-size>10</max-size>
              <min-size>0</min-size>
              <blocking-timeout-milliseconds>
                5000
              </blocking-timeout-milliseconds>
              <idle-timeout-minutes>30</idle-timeout-minutes>
              <match-one/>
            </single-pool>
          </connectionmanager>
        </connectiondefinition-instance>
      </connection-definition>
    </outbound-resourceadapter>
  </resourceadapter>
</connector>

The key attribute and elements here are:

configId

If this is deployed as a server-wide connection pool, the name specified here can be passed on the server command line to start the pool. In any case, it should be unique, and the configurations that ship with Geronimo use a format of product-group/product-name/version/type, as in some-company/my-product/1.0/jar.

parentId

Identifies the parent for this configuration. The parent is primarily important for ensuring that standalone RARs have access to the correct J2EE classes in their ClassLoader -- it should either be geronimo/j2ee-system/1.0/car or another configuration that has j2ee-system as an ultimate parent. For more information on the Geronimo ClassLoader hierarchy, see Section 4.4, “ClassLoaders in Geronimo”.

dependency

The dependency identifies a JAR that must be loaded for the connector to load -- this is generally used for the JDBC driver JAR. This element may appear more than once if multiple JARs need to be loaded.

uri

The path to the dependency JAR, relative to geronimo/repository. See Section 6.1, “JDBC Drivers”.

connectionfactory-interface

Must be javax.sql.DataSource

name

The name that other modules will use to refer to this connection pool. This must be unique within any given scope (server, application, etc.), and it will be easiest to configure references if this is further unique across all resources deployed at any scope in the server.

config-property-setting

Specifies a value for one of the configuration properties for the connector. The name attribute identifies the property, and the content of this element is the value to set for that property. The available properties are listed in Table 6.2, “Connection Pool Configuration Properties”.

max-size

The maximum number of simultaneous connections to allow for this connection pool.

min-size

The minimum size of the connection pool. If the pool falls below this size, it will be refilled to the minimum size. However, the pool is not initialized to this size until the first time a connection attempt is made. The default value is 0.

blocking-timeout-milliseconds

If a caller requests a connection and all the connections in the pool are in use, the caller will wait this long for a connection to become available. If the timeout expires without an available connection, an exception will be thrown.

idle-timeout-minutes

This interval defines how often the pool is checked for unused connections. If during that check any individual connection has been unused for at least this long, it will be closed and removed from the pool. The default value of 0 disables idle timeouts.

For a more extensive discussion of the connectionmanager settings, see Section 13.3.2.2.1, “Connection Manager Configuration”.

The configuration properties for the connection pool are listed in Table 6.2, “Connection Pool Configuration Properties”. Note that the properties should always be specified exactly as listed (many databases use different names for the username and password, but Geronimo will handle that).

Table 6.2. Connection Pool Configuration Properties

NameDescription
DriverThe class name of the JDBC driver
ConnectionURLThe JDBC URL used to connect to the database
UserNameThe user name used to connect to the database
PasswordThe password used to connect to the database
CommitBeforeAutocommitIf the JDBC driver does not commit pending work when setAutoCommit(true) is called, then this should be set to true to work around that. It's always safest to set it to true, but the performance is better for JDBC-compliant drivers when this is set to false (which is the default).
ExceptionSorterClassThe JDBC pool needs to know whether a given SQLException means that the connection has failed, or if it's just a normal SQL problem. This class is responsible for determining whether any given SQLException is fatal (and if so, the connection will be closed and released from the pool). In general, this is a product-specific decision, but product-specific implementations are not available as of Milestone 4. The current options are NoExceptionsAreFatalSorter or AllExceptionsAreFatalSorter (both in the package org.tranql.connector). This is mainly important if some SQLExceptions indicate or result in a broken database connection, and Geronimo should open new connections to compensate.
[Note]Note

It is not currently possible to pass arbitrary properties to the JDBC driver while making the connection. However, you may use a complex ConnectionURL if the driver allows additional properties to be added to the end of the URL instead.

6.4.2. Deploying a Database Pool

Once the database pool deployment plan has been prepared, you can deploy the pool using one of the three methods described here. In any of these cases, you'll need the deployment plan, and the TranQL RAR file.

6.4.2.1. Deploying a Server-Wide Database Pool

To deploy a server-wide database pool, you'll create a deployment plan using the format described in Section 6.4.1, “Configuring a Database Pool” and then deploy the connection pool using that plan.

First you should save the database pool deployment plan to a file like database-pool.xml (the name can be anything you like). Then you need to locate the TranQL RAR and deploy the pool using a command like this:

java -jar bin/deployer.jar deploy database-pool.xml \
     repository/tranql/rars/tranql-connector-1.1.rar

Note that this example assumes that the server is running, and it can be run from any directory, so long as you point it to the deployer JAR in geronimo/bin. The two arguments should point to the TranQL RAR file and the database pool deployment plan (the order doesn't matter). If the server is not running, you can use the command distribute instead of deploy though then you'll still have to start the configuration once the server is running. For more information on deployment commands, see Section 10.4, “The Deploy Tool”.

Other application modules can map resources to that database pool using the procedure described in Section 6.5, “Using a Database Pool”.

6.4.2.2. Deploying an Application-Scoped Database Pool

In contrast to server-wide database pools, application-scoped database pools are only visible to a single application by default, which must be packaged as an EAR.

To deploy an application-scoped database pool, you'll include the TranQL Connector RAR as a module in your application EAR (for normal JDBC pools; XA database pools would use a product-specific TranQL RAR, which should be in the same location.). First you should save the database pool deployment plan to a file like database-pool.xml (the name can be anything you like). Then you need to locate the TranQL RAR, and add both of these to your EAR.

For example, the EAR structure might look like this:

jar -tf my-app.ear
  my-web-app.war
  my-ejbs.jar
  tranql-connector-1.1.rar
  database-pool.xml
  META-INF/application.xml
  META-INF/geronimo-application.xml

Notice the TranQL RAR and the database configuration file. You should add a reference to the TranQL RAR to the standard META-INF/application.xml deployment descriptor, and add a reference to the database pool deployment plan to the META-INF/geronimo-application.xml deployment plan. This might be the only entry in the geronimo-application.xml file, so you'll need to create that file if you don't have one already.

The application.xml file for the EAR above would look like this:

META-INF/application.xml

<application 
       xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
       http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"
       version="1.4">
    <module>
        <ejb>my-ejbs.jar</ejb>
    </module>
    <module>
        <web>
            <web-uri>my-web-app.war</web-uri>
            <context-root>/my-web-app</context-root>
        </web>
    </module>
    <module>
        <connector>tranql-connector-1.1.rar</connector>
    </module>
</application>

Notice the final module entry for the RAR.

The geronimo-application.xml file identifying the database pool deployment plan would look like this:

META-INF/geronimo-application.xml

<application
       xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.0"
       configId="MyApplication">
    <module>
        <connector>tranql-connector-1.1.rar</connector>
        <alt-dd>database-plan.xml</alt-dd>
    </module>
</application>

Using the alt-dd element, the deployment plan for the application indicates that the database pool deployment plan for the connector is included as a separate file in the EAR, instead of being packaged into the RAR. The elements here look very similar to the elements in the standard application.xml deployment descriptor, but if we specify an alt-dd there it means an alternate standard J2EE deployment descriptor, whereas specifying the alt-dd here means an alternate Geronimo deployment plan for the connector.

For more information on the geronimo-application.xml file, see Chapter 15, Enterprise Applications (EARs) [DRAFT (1.0)].

6.4.2.3. Deploying a Client-Scoped Database Pool

The most restricted type of database pools, client-scoped database pools are only visible to the application client module that declares them. These pools are not visible to either other modules in the same application or to other applications in the same server.

To deploy a client-scoped database pool, you'll refer to a common copy of the TranQL Connector RAR (for normal JDBC pools) -- make sure it appears in geronimo/repository/tranql/rars. (XA database pools would use a product-specific TranQL RAR, which should be in the same location.)

A client-scoped connection pool is declared in the Geronimo deployment plan for the application client module that should have access to the pool. The plan will have an additional resource element containing the pool definition. A deployment plan that declared a pool using the same configuration shown in Example 6.1, “Database Pool Deployment Plan” would look like this:

META-INF/geronimo-application-client.xml

<application-client xmlns=
  "http://geronimo.apache.org/xml/ns/j2ee/application-client-1.0"
  configId="MyClientConfig"
  clientConfigId="MyClient">
  ...
  <resource>
    <external-rar>
      tranql/tranql-connector/1.1/rar
    </external-rar>
    <connector
     xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.0"
     version="1.5"
     configId="MyDatabase">
      <dependency>
        <uri>postgresql/pg74/213.jdbc3/jar</uri>
      </dependency>

      <resourceadapter>
        <outbound-resourceadapter>
          <connection-definition>
            <connectionfactory-interface>
              javax.sql.DataSource
            </connectionfactory-interface>
            <connectiondefinition-instance>
              <name>PostgreSQLDataSource</name>
              <config-property-setting name="UserName">
                dbuser
              </config-property-setting>
              <config-property-setting name="Password">
                dbpw
              </config-property-setting>
              <config-property-setting name="Driver">
                org.postgresql.Driver
              </config-property-setting>
              <config-property-setting name="ConnectionURL">
                jdbc:postgresql://localhost/mydb
              </config-property-setting>
              <config-property-setting
                                 name="CommitBeforeAutocommit">
                true
              </config-property-setting>
              <config-property-setting
                                  name="ExceptionSorterClass">
                org.tranql.connector.NoExceptionsAreFatalSorter
              </config-property-setting>
              <connectionmanager>
                <local-transaction/>
                <single-pool>
                  <max-size>10</max-size>
                  <min-size>0</min-size>
                  <blocking-timeout-milliseconds>
                    5000
                  </blocking-timeout-milliseconds>
                  <idle-timeout-minutes>30</idle-timeout-minutes>
                  <match-one/>
                </single-pool>
              </connectionmanager>
            </connectiondefinition-instance>
          </connection-definition>
        </outbound-resourceadapter>
      </resourceadapter>
    </connector>
  </resource>
</application-client>

The important elements here are:

resource

This element contains the entire definition of the client-scoped database pool

external-rar

This is formatted as a repository URI.

connector

This element contains all the data from the connector configuration file. The content is no different that the content of the deployment plan would have been for the server-wide or application-scoped database pools.

In the next section you'll see how to access that pool from application code.