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

340 lines
20 KiB
HTML
Raw Permalink 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="Managing connections" />
<meta name="abstract" content="It is important to be able to create, start, and end connections to your server. The following discussion explains concepts central to managing connections to your server and also offers some code examples." />
<meta name="description" content="It is important to be able to create, start, and end connections to your server. The following discussion explains concepts central to managing connections to your server and also offers some code examples." />
<meta name="DC.Relation" scheme="URI" content="progtips.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahh549.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahh552.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahh506.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahh550.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahh551.htm" />
<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="mngcon" />
<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>Managing connections</title>
</head>
<body id="mngcon"><a name="mngcon"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Managing connections</h1>
<div><p>It is important to be able to create, start, and end connections
to your server. The following discussion explains concepts central to managing
connections to your server and also offers some code examples.</p>
<div class="section"><p>To connect to an iSeries™ server, your Java™ program must create an <a href="as400obj.htm#as400obj">AS400</a> object.
The AS400 object contains up to one socket connection for each iSeries server
type. A service corresponds to a job on the server and is the interface to
the data on the server.</p>
<div class="note"><span class="notetitle">Note:</span> When you create Enterprise JavaBeans™ (EJB),
comply with the EJB specification that does not allow threads during your
connection. Although turning off IBM<sup>®</sup> Toolbox for Java thread support may slow your application,
it is required to comply with the EJB specification.</div>
<p>Every connection
to each server has its own job on the iSeries. A different server supports
each of the following:</p>
<ul><li>JDBC</li>
<li>Program call and command call</li>
<li>Integrated file system</li>
<li>Print</li>
<li>Data queue</li>
<li>Record-level access</li>
</ul>
<div class="note"><span class="notetitle">Note:</span> <ul><li>The <a href="print.htm#print">print</a> classes use one socket
connection per AS400 object if the application does not try to do two things
that require the network print server at the same time.</li>
<li>A <a href="print.htm#print">print</a> class creates additional
socket connections to the network print server if needed. The extra conversations
are disconnected if they are not used for 5 minutes.</li>
</ul>
</div>
</div>
<div class="section"><p>The Java program can control the number of connections to iSeries.
To optimize communications performance, a Java program can create multiple AS400 objects
for the same server as shown in Figure 1. This creates multiple socket connections
to the iSeries server.</p>
<p id="mngcon__mngconfig1"><a name="mngcon__mngconfig1"><!-- --></a><strong>Figure 1: Java program creating multiple AS400 objects
and socket connections for the same iSeries server</strong></p>
<p><img src="rzahh549.gif" longdesc="rzahh549.htm" alt="Java program creating multiple AS400 objects and socket connections for the same iSeries server" /> </p>
</div>
<div class="section"><p>To conserve server resources, create only one AS400 object as
shown in Figure 2. This approach reduces the number of connections, which
reduces the amount of resource used on the server.</p>
<p id="mngcon__fig2fig2"><a name="mngcon__fig2fig2"><!-- --></a><strong>Figure
2: Java program
creating a single AS400 object and socket connection for the same iSeries server</strong></p>
<p><img src="rzahh552.gif" longdesc="rzahh552.htm" alt="Java program creating a single AS400 object and socket connection for the same iSeries server" /> </p>
<div class="note"><span class="notetitle">Note:</span> Although
creating more connections increases the amount of resource used on the server,
creating more connections can have a benefit. Having more connections enables
your Java program
to process things in parallel, which can give better throughput (transactions-per-second)
and speed up your application.</div>
</div>
<div class="section"><p>You can also choose to use a connection pool to manage connections
as shown in Figure 3. This approach reduces the amount of time it takes to
connect to iSeries by
reusing a connection previously established for the user.</p>
<p id="mngcon__fig3fig3"><a name="mngcon__fig3fig3"><!-- --></a><strong>Figure
3: Java program
getting a connection from an AS400ConnectionPool to an iSeries server</strong></p>
<p><img src="rzahh506.gif" longdesc="rzahh506.htm" alt="Java program getting a connection from an AS400ConnectionPool to an iSeries server" /> </p>
</div>
<div class="section"><p>The following examples show how to create and use AS400 objects:</p>
<p><strong>Example
1:</strong> <span class="synph" id="mngcon__mngex1"><a name="mngcon__mngex1"><!-- --></a><span class="kwd"></span></span>In the following example, two
CommandCall objects are created that send commands to the same server. Because
the CommandCall objects use the same AS400 object, only one connection to
the server is created.</p>
<pre> // Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create two command call objects that use
// the same AS400 object.
CommandCall cmd1 = new CommandCall(sys,"myCommand1");
CommandCall cmd2 = new CommandCall(sys,"myCommand2");
// Run the commands. A connection is made when the
// first command is run. Since they use the same
// AS400 object the second command object will use
// the connection established by the first command.
cmd1.run();
cmd2.run();</pre>
<p><strong>Example 2:</strong> <span class="synph" id="mngcon__mngex2"><a name="mngcon__mngex2"><!-- --></a><span class="kwd"></span></span>In
the following example, two CommandCall objects are created that send commands
to the same iSeries server.
Because the CommandCall objects use different AS400 objects, two connections
to the server are created.</p>
<pre> // Create two AS400 objects to the same server.
AS400 sys1 = new AS400("mySystem.myCompany.com");
AS400 sys2 = new AS400("mySystem.myCompany.com");
// Create two command call objects. They use
// different AS400 objects.
CommandCall cmd1 = new CommandCall(sys1,"myCommand1");
CommandCall cmd2 = new CommandCall(sys2,"myCommand2");
// Run the commands. A connection is made when the
// first command is run. Since the second command
// object uses a different AS400 object, a second
// connection is made when the second command is run.
cmd1.run();
cmd2.run();</pre>
<p><strong>Example 3:</strong> <span class="synph" id="mngcon__mngex3"><a name="mngcon__mngex3"><!-- --></a><span class="kwd"></span></span>In
the following example, a CommandCall object and an IFSFileInputStream object
are created using the same AS400 object. Because the CommandCall object and
the IFSFileInput Stream object use different services on the iSeries server,
two connections are created.</p>
<pre> // Create an AS400 object.
AS400 newConn1 = new AS400("mySystem.myCompany.com");
// Create a command call object.
CommandCall cmd = new CommandCall(newConn1,"myCommand1");
// Create the file object. Creating it causes the
// AS400 object to connect to the file service.
IFSFileInputStream file = new IFSFileInputStream(newConn1,"/myfile");
// Run the command. A connection is made to the
// command service when the command is run.
cmd.run();</pre>
<p><strong>Example 4:</strong> <span class="synph" id="mngcon__mngex4"><a name="mngcon__mngex4"><!-- --></a><span class="kwd"></span></span>In
the following example, an AS400ConnectionPool is used to get an iSeries connection.
This example (like <a href="#mngcon__mngex3">Example 3</a> above) does
not specify a service, so the connection to the command service is made when
the command is run.</p>
<pre> // Create an AS400ConnectionPool.
AS400ConnectionPool testPool1 = new AS400ConnectionPool();
// Create a connection.
AS400 newConn1 = testPool1.getConnection("myAS400", "myUserID", "myPassword");
// Create a command call object that uses the AS400 object.
CommandCall cmd = new CommandCall(newConn1,"myCommand1");
// Run the command. A connection is made to the
// command service when the command is run.
cmd.run();
// Return connection to pool.
testPool1.returnConnectionToPool(newConn1);</pre>
<p id="mngcon__mngex5"><a name="mngcon__mngex5"><!-- --></a><strong>Example
5:</strong> The following example uses AS400ConnectionPool to connect to a particular
service when requesting the connection from the pool. This eliminates the
time required to connect to the service when the command is run (see <a href="#mngcon__mngex4">Example 4</a> above). If the connection is returned
to the pool, the next call to get a connection can return the same connection
object. This means that no extra connection time is needed, either on creation
or use.</p>
<pre> // Create an AS400ConnectionPool.
AS400ConnectionPool testPool1 = new AS400ConnectionPool();
// 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.))
AS400 newConn1 = testPool1.getConnection("myAS400", "myUserID", "myPassword", AS400.COMMAND);
// Create a command call object that uses the AS400 object.
CommandCall cmd = new CommandCall(newConn1,"myCommand1");
// Run the command. A connection has already been made
// to the command service.
cmd.run();
// Return connection to pool.
testPool1.returnConnectionToPool(newConn1);
// Get another connection to command service. In this case, it will return the same
// connection as above, meaning no extra connection time will be needed either now or
// when the command service is used.
AS400 newConn2 = testPool1.getConnection("myAS400", "myUserID", "myPassword", AS400.COMMAND);</pre>
</div>
<div class="section" id="mngcon__startendconnections"><a name="mngcon__startendconnections"><!-- --></a><h4 class="sectiontitle">Starting and ending connections</h4><p>The Java program
can control when a connection is started and ended. By default, a connection
is started when information is needed from the server. You can control exactly
when the connection is made by calling the <a href="javadoc/com/ibm/as400/access/AS400.html#CONNECTSERVICE(INT)"> connectService()</a> method on the AS400 object to preconnect
to the server.</p>
<p>Using an <a href="javadoc/com/ibm/as400/access/AS400ConnectionPool.html#NAVBAR_TOP"> AS400ConnectionPool</a>, you can create a connection preconnected
to a service without calling the connectService() method, as in <a href="#mngcon__mngex5">Example
5</a> above.</p>
<p>The following examples show Java programs connecting to and disconnecting
from iSeries.</p>
<p id="mngcon__mngex6"><a name="mngcon__mngex6"><!-- --></a><strong>Example 1:</strong> This example shows how to preconnect to iSeries:</p>
<pre> // Create an AS400 object.
AS400 system1 = new AS400("mySystem.myCompany.com");
// Connect to the command service. Do it now
// instead of when data is first sent to the
// command service. This is optional since the
// AS400 object will connect when necessary.
system1.connectService(AS400.COMMAND);</pre>
<p id="mngcon__mngex7"><a name="mngcon__mngex7"><!-- --></a><strong>Example
2:</strong> Once a connection is started, the Java program is responsible for disconnecting,
which is done either implicitly by the AS400 object, or explicitly by the Java program.
A Java program
disconnects by calling the <a href="javadoc/com/ibm/as400/access/AS400.html#DISCONNECTSERVICE(INT)"> disconnectService()</a> method on the AS400 object. To
improve performance, the Java program must disconnect only when the
program is finished with a service. If the Java program disconnects before it is finished
with a service, the AS400 object reconnects, if it is possible to reconnect,
when data is needed from the service.</p>
</div>
<div class="section"><p>Figure 4 shows how disconnecting the connection for the first
integrated file system object connection ends only that single instance of
the AS400 object connection, not all of the integrated file system object
connections.</p>
<p id="mngcon__fig4fig4"><a name="mngcon__fig4fig4"><!-- --></a><strong>Figure 4: Single object using its own
service for an instance of an AS400 object is disconnected</strong></p>
<p><img src="rzahh550.gif" longdesc="rzahh550.htm" alt="Single object using its own service for an instance of an AS400 object is disconnected" /> </p>
<p>This
example shows how the Java program disconnects a connection:</p>
<pre> // Create an AS400 object.
AS400 system1 = new AS400("mySystem.myCompany.com");
// ... use command call to send several commands
// to the server. Since connectService() was not
// called, the AS400 object automatically
// connects when the first command is run.
// All done sending commands so disconnect the
// connection.
system1.disconnectService(AS400.COMMAND);</pre>
</div>
<div class="section"><p id="mngcon__mngex8"><a name="mngcon__mngex8"><!-- --></a><strong>Example 3:</strong> Multiple objects that use the
same service and share the same AS400 object share a connection. Disconnecting
ends the connection for all objects that are using the same service for each
instance of an AS400 object as is shown in Figure 5.</p>
<p id="mngcon__fig5fig5"><a name="mngcon__fig5fig5"><!-- --></a><strong>Figure
5: All objects using the same service for an instance of an AS400 object
are disconnected</strong></p>
<p><img src="rzahh551.gif" longdesc="rzahh551.htm" alt="All objects using the same service for an instance of an AS400 object are disconnected" /> </p>
<p>For
example, two CommandCall objects use the same AS400 object. When disconnectService()
is called, the connection is ended for both CommandCall objects. When the
run() method for the second CommandCall object is called, the AS400 object
must reconnect to the service:</p>
<pre> // Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create two command call objects.
CommandCall cmd1 = new CommandCall(sys,"myCommand1");
CommandCall cmd2 = new CommandCall(sys,"myCommand2");
// Run the first command
cmd1.run();
// Disconnect from the command service.
sys.disconnectService(AS400.COMMAND);
// Run the second command. The AS400 object
// must reconnect to the server.
cmd2.run();
// Disconnect from the command service. This
// is the correct place to disconnect.
sys.disconnectService(AS400.COMMAND);</pre>
<p id="mngcon__mngex9"><a name="mngcon__mngex9"><!-- --></a><strong>Example
4:</strong> Not all IBM Toolbox
for Java
classes automatically reconnect. Some method calls in the <a href="ifs.htm#ifs">integrated
file system</a> classes do not reconnect because the file may have changed.
While the file was disconnected, some other process may have deleted the
file or changed its contents. In the following example, two file objects
use the same AS400 object. When disconnectService() is called, the connection
is ended for both file objects. The read() for the second IFSFileInputStream
object fails because it no longer has a connection to the server.</p>
<pre> // Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create two file objects. A connection to the
// server is created when the first object is
// created. The second object uses the connection
// created by the first object.
IFSFileInputStream file1 = new IFSFileInputStream(sys,"/file1");
IFSFileInputStream file2 = new IFSFileInputStream(sys,"/file2");
// Read from the first file, then close it.
int i1 = file1.read();
file1.close();
// Disconnect from the file service.
sys.disconnectService(AS400.FILE);
// Attempt to read from the second file. This
// fails because the connection to the file service
// no longer exists. The program must either
// disconnect later or have the second file use a
// different AS400 object (which causes it to
// have its own connection).
int i2 = file2.read();
// Close the second file.
file2.close();
// Disconnect from the file service. This
// is the correct place to disconnect.
sys.disconnectService(AS400.FILE);</pre>
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="rzahh549.htm">Long description of Figure 1: Java program creating multiple AS400 objects and socket connections for the same iSeries server (rzahh549.gif)</a></strong><br />
</li>
<li class="ulchildlink"><strong><a href="rzahh552.htm">Long description of Figure 2: Java program creating a single AS400 object and socket connection for the same iSeries server (rzahh552.gif)</a></strong><br />
</li>
<li class="ulchildlink"><strong><a href="rzahh506.htm">Long description of Figure 3: Java program getting a connection from an AS400ConnectionPool to an iSeries server (rzahh506.gif)</a></strong><br />
</li>
<li class="ulchildlink"><strong><a href="rzahh550.htm">Long description of Figure 4: Single object using its own service for an instance of an AS400 object is disconnected (rzahh550.gif)</a></strong><br />
</li>
<li class="ulchildlink"><strong><a href="rzahh551.htm">Long description of Figure 5: All objects using the same service for an instance of an AS400 object are disconnected (rzahh551.gif)</a></strong><br />
</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="progtips.htm" title="This section features a variety of tips that can help you use IBM Toolbox for Java.">Tips for programming</a></div>
</div>
</div>
</body>
</html>