19
Fri, Apr
5 New Articles

Pooling Party

Java
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

The Internet and Java have provided programmers with a double-edged sword. The two technologies offer a framework for building groundbreaking solutions, but their lack of maturity often has programmers writing functionality typically provided by older tools. Shared resource management is a good example. Fortunately, IBM’s WebSphere Application Server for AS/400 goes a long way toward providing services that programmers have come to expect from a good runtime environment. In the case of shared database resources, for example, WebSphere Application Server offers the IBM Connection Manager.

Database Interaction and Servlets

To connect to and interact with databases, Java programmers typically use the Java Database Connectivity (JDBC) API. The process is straightforward: You create a connection to the database through the Connection class, specify an SQL query string by using a Statement object, and scroll through the results of the SQL query via the ResultSet object returned by the Statement object. Connecting to the database can be expensive (usually several seconds), so most Java programs create a database connection once and reuse the connection throughout execution of the program.

Unfortunately, this is not a viable approach for servlet programmers. A servlet executes as a single process running a thread for each request, and each request requires its own database connection. Servlet performance would be poor if each request created its own connection, but because requests share resources such as instance variables, servlet programmers do not have the option of creating just one connection. If they did have that option, access to the connection would have to be synchronized, meaning that the servlet could handle only one request at a time.

To solve this problem, servlet programmers create a class that manages a pool of database connections. An instance of that class is initialized in the init() method of the servlet. When a request needs to access the database, the object typically returns a pooled connection, saving the request from the performance hit of a database connection. Connection Manager provides such an object. It also provides an administrative tool that monitors database connection statistics and has settings for tuning servlet performance to your specific connection needs.

The Connection Pool Model

Figure 1 shows the basic connection pool model. Rather than create its own database connection, the servlet requests a connection from a Connection Manager object, passing along a specification that describes the type of connection it needs. The specification details items such as the connection pool name and database name. Connection Manager then searches the pool for an existing but unused connection that matches the specification. If it finds one, that connection is returned.

Otherwise, Connection Manager creates a new connection, adds it to the pool, and returns it to the servlet.

Connection pools are configured in WebSphere to maintain a minimum number of connections. This minimum number creates a pool of connections when WebSphere starts up. Connection pools are also configured with a maximum number of connections so WebSphere starts another connection if all connections in the pool are busy but the number of connections has not yet reached that maximum. However, if a servlet requests a connection and the pool has already reached its maximum number of connections and all connections are in use, the servlet, depending on the connection pool configuration, waits until either a connection frees up or Connection Manager returns an error.

Pools are typically organized by connection properties such as database vendor and timeout value. WebSphere comes preconfigured with pools organized by database vendor for DB2, Oracle, Informix, SQL Server, and Sybase.

Setting Up a Connection Pool

Connection pools are configurable through the WebSphere Application Server administrator panel under section Setup and subsection Connection Management. You can access your AS/400’s WebSphere configuration panel from your Web browser by specifying your domain name or IP followed by port ID 9090 (http://199.140.33.5:9090). To configure WebSphere from the Internet, you must start the administration server via the Start TCP/IP Server (STRTCPSVR) command:

(STRTCPSVR SERVER(*HTTP) HTTPSVR(*ADMIN))

To ensure that WebSphere has been started, you also need to have an HTTP server instance started that uses WebSphere features. Figure 2 shows the WebSphere configuration screen.

When you click Connection Mgmt, a tabbed dialog appears on the right side of the screen. The connection pools are listed under this pool list tab. In creating a pool, you must specify the following settings (see Figure 3):

• The Pool Name setting is used by servlets to identify the connection pool.
• The Maximum Connections setting is the maximum number of connections maintained by the pool and is set at the maximum number of simultaneous active user requests that you expect will hit your database on any given day.

• The Minimum Connections setting is the minimum number of connections contained in the pool and is based on the minimum number of active user requests that you expect.

• The Connection Time Out setting specifies how long Connection Manager waits for a connection to become free when all connections in the pool are in use and the number of connections has reached the maximum. (A setting from 1,000 to 2,000 milliseconds is recommended.)

• The Maximum Age setting specifies the number of seconds for which an assigned connection can be idle before the reap process releases the connection from the servlet that acquired it.

• The Maximum Idle Time setting specifies the amount of time an unassigned connection remains in the pool before being marked for reclamation by the reap process.

• The Reap Time setting specifies how often (in seconds) the reap process runs. Then, there’s the reap process itself. Periodically, Connection Manager executes a “reap” process, which removes orphaned or idle connections. The Minimum Connections setting prevents Connection Manager from removing too many, and you always want some number around for the next set of servlet requests.

Using a Connection Pool

Connecting to a database through a connection pool requires initializing a Connection Manager object, creating a connection specification, and requesting a connection from the Connection Manager object. After your servlet obtains a connection from the pool, you can use the connection just as you would any JDBC connection. However, it is important to release the connection after you are finished with it. Otherwise, the connection is used only once and hangs around until it times out and is reclaimed by the reap process. This connection hoarding eliminates the benefits of having a pool of connections and kills performance because valuable system resources are not released.

To use connection pools, your servlet needs the following instance variables:

protected IBMConnMgr connectionManager;
protected IBMJdbcConnSpec connectionSpec;
protected IBMJdbcConn connection;

IBM provides classes IBMJdbcConnMgr, IBMJdbcConnSpec, and IBMJdbcConn as part of WebSphere. You use an instance of IBMJdbcConnMgr to request connections. (To create an instance of this class, use a static method of the IBMConnMgrUtil class.) An instance of IBMJdbcConnSpec defines your connection specification; pass this connection to Connection Manager every time you request a connection to a database. The connection returned is an object of IBMJdbcConn and contains the underlying JDBC connection. The object also provides a method that releases the connection back into the connection pool. These classes are all in package com.ibm.servlet. connmgr.

Your init() method should contain code that gets the Connection Manager object and code that creates the connection specification object:

connectionManager = IBMConnMgrUtil.getIBMConnMgr();
connectionSpec = new IBMJdbcConnSpec(poolName, aBoolean,
jdbcDriverName,
jdbcConnectionURL,
userName, password);

The static getIBMConnMgr method creates the Connection Manager object for you. To create an instance of IBMJdbcConnSpec, use the standard constructor. The Boolean passed in specifies whether or not the servlet waits for connections. Normally, you pass in “true”; all other parameters are character strings.

In the service() (or doGet() or doPost(), depending on your servlet design) method, you use the IBM connection manager and connection specifi-cation objects to get an instance of IBMJdbcConn. You then use that object to get your JDBC connection:

connection =
(IBMJdbcConn)connectionMgr.getIBMConnection(connectionSpec);
java.sql.Connection conn = connection.getJdbcConnection();
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rs = stmt.executeQuery(“Select...”);
... process data from result set ...
connection.releaseIBMConnection();

When you are done, you must release the connection by using the release IBMConnection() method.

Party Line

Bit by bit, IBM and others are providing the tools and support serious developers require for business-critical applications. For more information on Connection Manager, read Chapter 7 of IBM WebSphere and VisualAge for Java Database Integration with DB2, Oracle, and SQL Server. You can access this Redbook online at www.redbooks.ibm.com. Read the book. Go through a few tutorials. Add yourself to the growing ranks of master servlet developers.

Related Material

IBM WebSphere and VisualAge for Java Database Integration with DB2, Oracle, and SQL Server, Redbook (SG24-5471-00)

DB2/400

Oracle

JdbcDb2 pool

Connection JdbcOracle pool Connection

Figure 1: Here's the relationship between components when they use connection pools.

Connection

Request

JDBC Connection

Connection

Connection Connection

Connection Manager

Connection Connection

Connection

Servlet




Figure 2: WebSphere's Web-based configuration provides a GUI alternative to manually entering values on a properties file.


Figure 3: The Connection Manager dialog prompts all pool connection options.



Pooling_Party05-00.png 668x604





Pooling_Party05-01.png 672x468
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: