ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/connectpoolexample.htm

163 lines
7.5 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Using AS400ConnectionPool" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="connectpoolexample" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Using AS400ConnectionPool</title>
</head>
<body id="connectpoolexample"><a name="connectpoolexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using AS400ConnectionPool</h1>
<div><p></p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm#codedisclaimer">Code
example disclaimer</a> for important legal information.</div>
<pre>//////////////////////////////////////////////////////////////////////////////////
//
// AS400ConnectionPooling example. This program uses an AS400ConnectionPool to
// create connections to an iSeries server.
// Command syntax:
// AS400ConnectionPooling system myUserId myPassword
//
// For example,
// AS400ConnectionPooling MySystem MyUserId MyPassword
//
//////////////////////////////////////////////////////////////////////////////////
import com.ibm.as400.access.*;
public class AS400ConnectionPooling
{
public static void main (String[] parameters)
{
// Check the input parameters.
if (parameters.length != 3)
{
System.out.println("");
System.out.println("Usage:");
System.out.println("");
System.out.println(" AS400ConnectionPooling system userId password");
System.out.println("");
System.out.println("");
System.out.println("For example:");
System.out.println("");
System.out.println("");
System.out.println(" AS400ConnectionPooling MySystem MyUserId MyPassword");
System.out.println("");
return;
}
String system = parameters[0];
String userId = parameters[1];
String password = parameters[2];
try
{
// Create an AS400ConnectionPool.
AS400ConnectionPool testPool = new AS400ConnectionPool();
// Set a maximum of 128 connections to this pool.
testPool.setMaxConnections(128);
// Set a maximum lifetime for 30 minutes for connections.
testPool.setMaxLifetime(1000*60*30); // 30 min Max lifetime since created.
// Preconnect 5 connections to the AS400.COMMAND service.
testPool.fill(system, userId, password, AS400.COMMAND, 1);
System.out.println();
System.out.println("Preconnected 1 connection to the AS400.COMMAND service");
// Call getActiveConnectionCount and getAvailableConnectionCount to see how many
// connections are in use and available for a particular system.
System.out.println("Number of active connections: "
+ testPool.getActiveConnectionCount(system, userId));
System.out.println("Number of available connections for use: "
+ testPool.getAvailableConnectionCount(system, userId));
// Create a connection to the AS400.COMMAND service. (Use the service number
// constants defined in the AS400 class (FILE, PRINT, COMMAND, DATAQUEUE, and so on.))
// Since connections have already been filled, the usual time spent connecting
// to the command service is avoided.
AS400 newConn1 = testPool.getConnection(system, userId, password, AS400.COMMAND);
System.out.println();
System.out.println("getConnection gives out an existing connection to user");
System.out.println("Number of active connections: "
+ testPool.getActiveConnectionCount(system, userId));
System.out.println("Number of available connections for use: "
+ testPool.getAvailableConnectionCount(system, userId));
// Create a new command call object and run a command.
CommandCall cmd1 = new CommandCall(newConn1);
cmd1.run("CRTLIB FRED");
// Return the connection to the pool.
testPool.returnConnectionToPool(newConn1);
System.out.println();
System.out.println("Returned a connection to pool");
System.out.println("Number of active connections: "
+ testPool.getActiveConnectionCount(system, userId));
System.out.println("Number of available connections for reuse: "
+ testPool.getAvailableConnectionCount(system, userId));
// Create a connection to the AS400.COMMAND service. This will return the same
// object as above for reuse.
AS400 newConn2 = testPool.getConnection(system, userId, password, AS400.COMMAND);
System.out.println();
System.out.println("getConnection gives out an existing connection to user");
System.out.println("Number of active connections: "
+ testPool.getActiveConnectionCount(system, userId));
System.out.println("Number of available connections for reuse: "
+ testPool.getAvailableConnectionCount(system, userId));
// Create a connection to the AS400.COMMAND service. This will create a new
// connection as there are not any connections in the pool to reuse.
AS400 newConn3 = testPool.getConnection(system, userId, password, AS400.COMMAND);
System.out.println();
System.out.println("getConnection creates a new connection because there are no
connections available");
System.out.println("Number of active connections: "
+ testPool.getActiveConnectionCount(system, userId));
System.out.println("Number of available connections for reuse: "
+ testPool.getAvailableConnectionCount(system, userId));
// Close the test pool.
testPool.close();
}
catch (Exception e)
{
// If any of the above operations failed say the pool operations failed
// and output the exception.
System.out.println("Pool operations failed");
System.out.println(e);
e.printStackTrace();
}
}
}</pre>
</div>
</div>
</body>
</html>