ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/dataecwt.htm

157 lines
5.3 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>ConnectionWaitTimeout exception</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h6><a name="dataecwt"></a>ConnectionWaitTimeout exception</h6>
<p>The ConnectionWaitTimeout exception indicates that the application has waited for the number of seconds specified by the connection timeout setting and has not received a connection. This situation can occur when the pool is at maximum size and all of the connections are in use by other applications for the duration of the wait. In addition, there are no connections currently in use that the application can share because either the connection properties do not match, or the connection is in a different transaction.</p>
<ul>
<li><p>When using a Version 4.0 data source, the ConnectionWaitTimeout throws an exception whose class is com.ibm.ejs.cm.pool.ConnectionWaitTimeoutException.</p></li>
<li><p>For connection factories, the ConnectionWaitTimeout throws a ResourceException whose class is com.ibm.websphere.ce.j2c.ConnectionWaitTimeoutException.</p></li>
<li><p>Finally, WebSphere Application Server - Express data sources throw an SQLException subclass called com.ibm.websphere.ce.cm.ConnectionWaitTimeoutException.</p></li>
</ul>
<p>These code examples show how to use the ConnectionWaitTimoutException with the JDBC API and the J2EE Connector Architecture (JCA) API.</p>
<p>See the <a href="codex.htm">Code example disclaimer</a> for legal information about these code examples.</p>
<p><strong>Example: Using ConnectionWaitTimeoutException for the JDBC API</strong></p>
<p>In all cases in which the ConnectionWaitTimeoutException is caught, there is very little to do for recovery. The following code fragment shows how to use this exception in the JDBC API:</p>
<pre>import java.sql.*;
import java.naming.*;
import javax.rmi.*;
public void test1() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Look for datasource
java.util.Properties props = new java.util.Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
&quot;com.ibm.websphere.naming.WsnInitialContextFactory&quot;);
ic = new InitialContext(props);
DataSource ds1 =
(DataSource) PortableRemoteObject.narrow(ic.lookup(jndiString), DataSource.class);
// Get Connection.
conn = ds1.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(&quot;select * from mytable where this = 54&quot;);
}
catch (com.ibm.websphere.ce.cm.ConnectionWaitTimeoutException cwte) {
// Notify the user that the system could not provide a connection
// to the database. This usually happens when the connection pool
// is full and there is no connection available to share.
}
catch (SQLException sqle) {
// Handle other database problems.
}
finally {
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqle1) {
}
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqle1) {
}
}
if (conn != null)
try {
conn.close();
}
catch (SQLException sqle1) {
}
}
}
}</pre>
<p><strong>Example: Using ConnectionWaitTimeoutException for the JCA API</strong></p>
<p>In all cases in which the ConnectionWaitTimeoutException is caught, there is very little to do for recovery. The following code fragment shows how to use this exception in JCA:</p>
<pre>import javax.naming.*;
import javax.resource.*;
import javax.resource.cci.*;
import javax.rmi.*;
/**
* This method does a simple Connection test.
*/
public void testConnection() throws NamingException, ResourceException,
com.ibm.websphere.ce.j2c.ConnectionWaitTimeoutException {
ConnectionFactory factory = null;
Connection conn = null;
ConnectionMetaData metaData = null;
try {
// lookup the connection factory
if (verbose) System.out.println(&quot;Look up the connection factory...&quot;);
try {
factory = (ConnectionFactory) PortableRemoteObject.narrow(
(new InitialContext()).lookup(&quot;java:comp/env/eis/Sample&quot;),
ConnectionFactory.class);
}
catch (NamingException ne) {
// Connection factory cannot be looked up.
throw ne;
}
// Get connection
if (verbose) System.out.println(&quot;Get the connection...&quot;);
conn = factory.getConnection();
// Get ConnectionMetaData
metaData = conn.getMetaData();
// Print out the metadata Information.
System.out.println(&quot;EISProductName is &quot; + metaData.getEISProductName());
}
catch (com.ibm.websphere.ce.j2c.ConnectionWaitTimeoutException cwtoe) {
// Connection Wait Timeout
throw cwtoe;
}
catch (ResourceException re) {
// Something wrong with connections.
throw re;
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (ResourceException re) {
}
}
}
}</pre>
</body>
</html>