Problem solve Get help with specific problems with your technologies, process and projects.

Using connection pooling in JCo

In the world of database connectivity, creating individual connections to a database system is expensive.

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());
     }
     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 last published in March 2003

Dig Deeper on SAP Java and J2EE

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close