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.
When using a Version 4.0 data source, the ConnectionWaitTimeout throws an exception whose class is com.ibm.ejs.cm.pool.ConnectionWaitTimeoutException.
For connection factories, the ConnectionWaitTimeout throws a ResourceException whose class is com.ibm.websphere.ce.j2c.ConnectionWaitTimeoutException.
Finally, WebSphere Application Server - Express data sources throw an SQLException subclass called com.ibm.websphere.ce.cm.ConnectionWaitTimeoutException.
These code examples show how to use the ConnectionWaitTimoutException with the JDBC API and the J2EE Connector Architecture (JCA) API.
See the Code example disclaimer for legal information about these code examples.
Example: Using ConnectionWaitTimeoutException for the JDBC API
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:
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, "com.ibm.websphere.naming.WsnInitialContextFactory"); 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("select * from mytable where this = 54"); } 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) { } } } }
Example: Using ConnectionWaitTimeoutException for the JCA API
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:
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("Look up the connection factory..."); try { factory = (ConnectionFactory) PortableRemoteObject.narrow( (new InitialContext()).lookup("java:comp/env/eis/Sample"), ConnectionFactory.class); } catch (NamingException ne) { // Connection factory cannot be looked up. throw ne; } // Get connection if (verbose) System.out.println("Get the connection..."); conn = factory.getConnection(); // Get ConnectionMetaData metaData = conn.getMetaData(); // Print out the metadata Information. System.out.println("EISProductName is " + 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) { } } } }