Nucleus components use a connection pool to obtain a connection when one is needed, and then return the connection after the database transaction has finished. You can obtain a DataSource by making the DataSource connection pool a property of a component or by using JNDI (Java Naming and Directory Interface) lookup, as described in the following sections.

Making the Connection Pool a Property

If you have connection pool code that is either in a component or that extends atg.nucleus.GenericService, the easiest way to get a connection pool is to make the connection pool a property of the component. Assign the appropriate pool to the property so that it will be available for use when the component is created.

For example, let’s say you’ve created a component called /atg/dynamo/test/MyTestComponent. To add the connection pool as a property of the component, add property accessor methods similar to the following:

  // Make the DataSource a property of this component.
  DataSource connectionPool;
  public DataSource getConnectionPool()
  { return connectionPool; }
  public void setConnectionPool (DataSource connectionPool)
  { this.connectionPool = connectionPool; }

To point the component to a connection pool, create a MyTestComponent.properties file similar to this:

$class=YourClassName
connectionPool=/atg/dynamo/service/jdbc/YourDataSource

To point the component to the default connection pool, for example, the MyTestComponent.properties file would look like this:

$class=YourClassName
connectionPool=/atg/dynamo/service/jdbc/JTDataSource

For more information about using properties files to initialize components, see the Nucleus: Organizing JavaBean Components chapter in the ATG Programming Guide.

Using JNDI Lookup

Dynamo provides JNDI access to all components in the Nucleus hierarchy. If necessary, you can configure code with the JNDI path of the desired pool so that it looks up the pool as needed. To access the default connection pool component, for example, the code is as follows:

String jndiName = "dynamo:/atg/dynamo/service/jdbc/JTDataSource";
Context ctx = new javax.naming.InitialContext ();
DataSource ds = (DataSource) ctx.lookup (jndiName);

To access a connection from a connection pool, use code similar to the following:

DataSource ds = getConnectionPool();
Connection c = null;
try {
  try {
    c = ds.getConnection();

         // Use the connection without calling c.commit() or c.rollback().
          ... use the connection ...
  }
  finally {
    // Closing of the Connection must always occur or else
    // connections will not be returned to the pool properly!
    if (c != null)
    c.close();
  }
}
catch (SQLException sqle) {
// If something went wrong, you might want to roll back
// the transaction, depending on the error that you receive.
}

Note: For information about rolling back a transaction, see the Transaction Management chapter of the ATG Programming Guide.

Although this example may seem overly complex, it provides the level of robustness you need for a real database application. All exceptions are caught and logged. More important, connection objects are always returned to the pool, which avoids the possibility of all connection objects being locked out because services forgot to return them. Your application enjoys the additional performance provided by reusing JDBC connection objects, and also has a place where those database connections can be configured and monitored.

 
loading table of contents...