Using connection pooling in JCo

Using connection pooling in JCo

In the world of database connectivity, creating individual connections to a database system is technically expensive and often incurs unwanted overhead in both client and server-side applications. Connection pools allow your application to check connections out from a pool and release a connection once the transaction has completed. This tip briefly explores the value and implementation of using connection pools in JCo.

The following code presents two classes that demonstrate the use of a connection pool and its restrictions. You should be able to pull code from this example when adding pooling to your own Java/JCo application.

The first JCo application does not use pooling:

import com.sap.mw.jco.*;

public class JcoTest {
 private static JCO.Client theConnection;
 private static IRepository theRepository;
  
 public static void main(String[] args) {
  createConnection();
  retrieveRepository();  
  try {
  JCO.Function function = getFunction("RFC_READ_TABLE");
  JCO.ParameterList listParams = function.getImportParameterList();
  
  listParams.setValue("BSAUTHORS", "QUERY_TABLE");
  
  theConnection.execute(function);
  
  JCO.Table tableList = function.getTableParameterList().getTable("DATA");
  
  if (tableList.getNumRows() > 0) {
   do {
    for (JCO.FieldIterator fI = tableList.fields();
      fI.hasMoreElements();)
     {
      JCO.Field tabField = fI.nextField();
      System.out.println(tabField.getName()
           + ":t" +
           tabField.getString());

    Requires Free Membership to View

    When you register, you will start receiving targeted emails from my award-winning team of editorial writers. Our goal is to keep you informed on the hottest topics and biggest challenges faced by SAP professionals today.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSAP.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSAP.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

} System.out.println("n"); } while (tableList.nextRow() == true); } } catch (Exception ex) { ex.printStackTrace(); } } private static void createConnection() { try { theConnection = JCO.createClient("000", "DDIC", "minisap", "en", "sincgo", "00"); theConnection.connect(); } catch (Exception ex) { System.out.println("Failed to connect to SAP system"); } } private static void retrieveRepository() { try { theRepository = new JCO.Repository("saprep", theConnection); } catch (Exception ex) { System.out.println("failed to retrieve repository"); } } public static JCO.Function getFunction(String name) { try { return theRepository.getFunctionTemplate(name.toUpperCase()).getFunction(); } catch (Exception ex) { ex.printStackTrace(); } return null; } }

To add connection pooling you simply add this code to the beginning of the main method:

    if (connPool == null) {
      JCO.addClientPool(POOL_NAME,
                        5,
                        "000",
                        "bcuser",
                        "minisap",
                        "EN",
                        "sincgo",
                        "00");
    } 
 

And replace the call to the createConnection() method with this:

         theConnection = JCO.getClient(POOL_NAME);

The code should now look like this:

import com.sap.mw.jco.*;

public class JcoTest {
 private static JCO.Client theConnection;
 private static IRepository theRepository;
    private static final String POOL_NAME = "myPool";
      
 public static void main(String[] args) {
  
    JCO.Pool connPool = JCO.getClientPoolManager().getPool(POOL_NAME);
    if (connPool == null) {
      JCO.addClientPool(POOL_NAME,
                        5,      //number of connections in the pool
                        "client",
                        "username",
                        "paswword",
                        "EN",
                        "hostname",
                        "00");
    }  
    
        theConnection = JCO.getClient(POOL_NAME);
  retrieveRepository();  
  try {
  JCO.Function function = getFunction("RFC_READ_TABLE");
  JCO.ParameterList listParams = function.getImportParameterList();
  
  listParams.setValue("BSAUTHORS", "QUERY_TABLE");
  
  theConnection.execute(function);
  
  JCO.Table tableList = function.getTableParameterList().getTable("DATA");
  
  if (tableList.getNumRows() > 0) {
   do {
    for (JCO.FieldIterator fI = tableList.fields();
      fI.hasMoreElements();)
     {
      JCO.Field tabField = fI.nextField();
      System.out.println(tabField.getName()
           + ":t" +
           tabField.getString());
     }
     System.out.println("n");
   }
   while (tableList.nextRow() == true);
  }
  }
  catch (Exception ex) {
   ex.printStackTrace();
  }
  
  JCO.releaseClient(theConnection);
  
 }
 
 private static void retrieveRepository() {
  try {
   theRepository = new JCO.Repository("saprep", theConnection);
  }
  catch (Exception ex)
  {
   System.out.println("failed to retrieve repository");
  }
 }
  public static JCO.Function getFunction(String name) {
    try {
         return theRepository.getFunctionTemplate(name.toUpperCase()).getFunction();
    }
    catch (Exception ex) {
     ex.printStackTrace();
    }
      return null;
    }  
}

Now, when this class executes, it will create a pool of 5 SAP connections. Once created, a single connection is retrieved and executed against. After the call to SAP has finished, the connections are released with:

JCO.releaseClient(theConnection);
You can easily integrate this code in any application that acts as a server or requires multiple users to access SAP via the Web. Author Austin Sincock is a freelance Java/SAP consultant who contributes regularly to Web and print journals. He can be reached at austin@opensourceguru.com. Check out his upcoming book Enterprise Java for SAP

This was first published in March 2003

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.