• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Connection Pooling with Tomcat

    June 26, 2002 Timothy Prickett Morgan

    Hey, David:

    First of all, let me thank you for a very good article in the February 14 issue of Midrange Guru, OS/400 Edition (see “Installing and Configuring Tomcat on iSeries“). Your article helped me to understand quite a few things.

    I would like to ask you a question about the following statement you made: “Tomcat does provide a mechanism to pool connections….” Could you please explain what that mechanism is and how it could be accessed?

    At my workplace, we installed Tomcat and are trying to begin development. We have to deal with AS/400 databases, and connection pooling remains a very unclear point, yet that was one of the major selling points to get Tomcat in our shop in the first place. Help us, please.

    — Leo

    I am glad to hear that the Tomcat article was helpful. Connection pooling in Tomcat is not that difficult once you find the documentation and have an example. I have used Tomcat for a number of projects and so far it has been easy to set up, stable, and fast. You do not mention which version of Tomcat you are using, but I would recommend that you use the latest version (4.0.4 today) and run Tomcat standalone.

    Your first question asks what mechanism Tomcat uses for connection pooling. That mechanism is the open source Tyrex package. Among other things, the Tyrex package provides a full-featured JDBC pool implementation. At this point, it appears that Tomcat 4.1 will be switching to the Jakarta commons DBCP package.

    The Tomcat JNDI Resources HOW-TO gives an overview of Tomcat’s connection pool support. That page describes the steps you must follow to set up Tomcat’s connection pooling. One thing missing from that page is an iSeries example and step-by-step instructions.

    The first thing you need to do is to install Tomcat. Next, I would download the latest version of JTOpen and install the JAR files; today that is JTOpen 3.1 (which requires a user name and password). You might also want to review the Midrange Guru article, “Avoiding a Sign-On with Java .”

    After you have Tomcat, Java, and JTOpen set up, you are ready to set up your connection pool. Start by configuring a JNDI resource by adding the following lines to Tomcat’s server.xml file, as in this example:

    <Context path="" docBase="/appdir" debug="0"/>
      <Resource name="jdbc/db2"
                auth="Container"
                type="javax.sql.DataSource"/>
      <ResourceParams name="jdbc/db2">
                                     
        <parameter>
          
          <name>driverClassName</name>
    
          <value>com.ibm.db2.jdbc.app.DB2Driver</value>
    
        </parameter>
               
        <parameter>
                
          <name>driverName</name>
    
          <value>jdbc:db2://localhost;naming=system;
               libraries=,lib1,lib2;translate binary=true</value>
        </parameter>
               
      </ResourceParams>
            
    </Context>
    

    In that example, I assigned the name jdbc/db2. You can use any name you would like and can create different data sources for different drivers and JDBC URLs. The JDBC URL I used in that example specifies the native driver. If you want to use the toolbox driver (for testing from your PC for example), you would change the URL to something like the following example:

    jdbc:as400://yourhost.com;naming=system;libraries=,lib1,lib2
    

    In the next step, declare the JNDI name in your web.xml application deployment descriptor. The web.xml file is in the WEB-INF directory of your application (where the Context docBase in server.xml points). That declaration should look something like the following example:

    <resource-ref>
      <description>
        Resource reference to java.sql.Connection
        factory defined in server.xml
      </description>
      <res-ref-name>jdbc/db2</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>
    

    Finally, you will need to build retrieve connections from the pool. You can do this in each class that needs a connection, but a better alternative is to set up a class that returns connections. I use a static method to retrieve pool entries. Here is an example:

    package com.yourdomain.db;
    
    import com.yourdomain.util.ApplicationError;
    import java.sql.Connection;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    
    /**
     * Class Pool Returns a connection from a JNDI data source.
     * 
     * @author David Morris
     */
    public class Pool {
    
        /**
         * Method Pool create a new connection pool
         */
        public Pool() {
            System.err.println("DBUtil instance created.");
        }
    
        /**
         * Method getConnection.
         * @return Connection New connection from the pool
         */
        public static Connection getConnection() {
    
            Connection conn = null;
    
            try {
                Context ctx = (Context) new InitialContext().
                     lookup("java:comp/env");
                conn = ((DataSource) ctx.lookup("jdbc/db2")).
                     getConnection();
            }
            catch (Exception e) {
                e.printStackTrace(System.err);
                throw new ApplicationError(e);
            }
    
            return conn;
        }
    }
    

    You use the Pool class to retrieve connections in your applications using something like this example:

    Connection conn = Pool.getConnection();
    try {
        PreparedStatement ps = 
            conn.prepareStatement("select * from file");
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            ...
        }
    }
    catch (SQLException e) {
        throw new ApplicationError(e);
    }
    finally {
        try {
            conn.close();
        }
        catch (SQLException e2) {
            throw new ApplicationError(e2);
        }
    }
    

    Hopefully this will get you started.

    — David

    Sponsored By
    WORKSRIGHT SOFTWARE

    On June 30, 2002,
    $$$$$$$$    Postal Rates will go UP!    $$$$$$$$

    On Monday, July 1st,
    $$$$$    your postage bill
    can go down.    $$$$$

    How can this happen? By CASS certifying your mailing names
    and addresses and presorting your outgoing mail. Our CASS certification software ensures that your address files
    have valid ZIP Code and address information. Our presort software ensures that you can properly prepare you mail
    for delivery to your Post Office.

    WorksRight Software,
    Inc. is the number-one source for iSeries and AS/400 CASS,
    presort, ZIP Code, and area code software and data.

    Visit our Web site – www.worksright.com –
    to learn more about our CASS and presorting software, or contact WorksRight Software, Inc., phone 601-856-8337,

    e-mail software@worksright.
    com
    .

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: mgo_rc, Volume 2, Number 49 -- June 26, 2002

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Parameterized RUNSQLSTM Determining Whether a User Is Already Signed On, Take Three

    Leave a Reply Cancel reply

MGO Volume: 2 Issue: 49

This Issue Sponsored By

    Table of Contents

    • Searching Message Text
    • Connection Pooling with Tomcat
    • Service Program Catch-22

    Content archive

    • The Four Hundred
    • Four Hundred Stuff
    • Four Hundred Guru

    Recent Posts

    • Liam Allan Shares What’s Coming Next With Code For IBM i
    • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
    • VS Code Will Be The Heart Of The Modern IBM i Platform
    • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
    • IBM i PTF Guide, Volume 27, Number 25
    • Meet The Next Gen Of IBMers Helping To Build IBM i
    • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
    • Will Independent IBM i Clouds Survive PowerVS?
    • Now, IBM Is Jacking Up Hardware Maintenance Prices
    • IBM i PTF Guide, Volume 27, Number 24

    Subscribe

    To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

    Pages

    • About Us
    • Contact
    • Contributors
    • Four Hundred Monitor
    • IBM i PTF Guide
    • Media Kit
    • Subscribe

    Search

    Copyright © 2025 IT Jungle