Defining connection caches  Using Connection Manager routines in C, C++, and ActiveX components

Chapter 26: Using Connection Management

Using Java Connection Manager classes

Java components can use the Java Connection Manager (JCM) classes to take advantage of connection caching. The JCM classes manage JDBC connections.


The JCM classes are:

These classes are documented in Chapter 1, “Java Classes and Interfaces,” in the EAServer API Reference.

Java Connection Manager example

The example below implements a Java component that calls these JCM class methods:

Many JDBC programs do not explicitly clean up java.sql.Statement objects. Instead, they rely on the JDBC driver to clean up Statement objects when the connection is closed. This strategy does not work with cached connections; you must explicitly clean up Statement objects before releasing a connection back into the cache. To clean up Statement objects, call Statement.close() and set the Statement reference to null.

WARNING! To prevent memory leaks, you must explicitly clean up a connection’s Statement objects before releasing the connection back into the cache. Do not release the same connection more than once.

This code also calls JContext.forwardResultSet(ResultSet) to forward result sets from a remote server to the client:


import java.sql.*;
import java.util.*;

 *  Java class to implement rs_passthru EAServer component.
class rs_passthruImpl {
  JCMCache _cache = null;
  private static final String _user = "dba";
  private static final String _password = "sql";
  private static final String _server_url = 
                       "jdbc:odbc:Jaguar SVU Sample";

   * Default constructor that is called by EAServer
   *  when a component instance is created.
  public rs_passthruImpl() throws JException {

    // Get a JDBC connection cache handle.
    try {
      _cache = JCM.getCache(_user, _password,
    } catch (Exception e) {
      Jaguar.writeLog(true, “rs_passthru(): getCache() exception”
                      + e.getMessage());
      _cache = null;
    // If we can’t get a cache handle here, log an
    // error message then throw an exception.
    if (_cache == null) 
        "rs_passthru(): Could not access connection cache.”);
      Jaguar.writeLog(false, "rs_passthru(): Cache may not be configured properly in Jaguar Manager.");
      throw new JException(
        "rstest(): Could not create connection cache.");
  } // rs_passthruImpl()
   * Forward the client’s query to the remote server, forward
   * the results back to the client.
  public void passthru_query (String query)
     throws JException, SQLException
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    // Note that this code does not catch exceptions; 
    // if an exception is thrown will be caught by EAServer 
    // and the method invocation will fail.

    // Call getConnection() to get a connection from the cache.
    while (conn == null)
      conn = _cache.getConnection(0);
    // Create a Statement instance and use it to 
    // forward the query.
    stmt = conn.createStatement();
    boolean results = stmt.execute(query);
    int update_count = -1;
    // Process all the results, forwarding each result set 
    // to the client.
    do {
      if (results)
        rs = stmt.getResultSet();
        if (rs != null)
        update_count = stmt.getUpdateCount();
      results = stmt.getMoreResults();
    } while (results || (update_count != -1));
    // Explicitly release the Statement object. 
    // Otherwise, it will linger attached to the cached
    // connection.
    stmt = NULL;
  } // passthru_query (String)

} // class rs_passthruImpl

For more Java connection management examples, see the source for the EAServer sample Java components in your installation directory.

Copyright © 2005. Sybase Inc. All rights reserved. Using Connection Manager routines in C, C++, and ActiveX components