+

Search Tips   |   Advanced Search

Example: Handling data access exception - ConnectionWaitTimeoutException (for the JDBC API)


This code sample demonstrates how specify the conditions under which the appserver issues the ConnectionWaitTimeoutException for a JDBC application.

In all cases in which the ConnectionWaitTimeoutException is caught, there is very little that can be done to recover.

public void test1() {
          java.sql.Connection conn = null;
          java.sql.Statement stmt = null;
          java.sql.ResultSet rs = null;

          try {
               
// Look for datasource
               java.util.Properties props = new java.util.Properties();
               props.put(
                    javax.naming.Context.INITIAL_CONTEXT_FACTORY,                     
                    "com.ibm.websphere.naming.WsnInitialContextFactory");
               ic = new javax.naming.InitialContext(props);
               javax.sql.DataSource ds1 = (javax.sql.DataSource) ic.lookup(jndiString);

               
// Get Connection.
               conn = ds1.getConnection();
               stmt = conn.createStatement();
               rs = stmt.executeQuery("select * from mytable where this = 54");
          }
          catch (java.sql.SQLException sqlX) {
          if (sqlX instanceof com.ibm.websphere.ce.cm.ConnectionWaitTimeoutException
           || sqlX instanceof java.sql.SQLTransientConnectionException
              && sqlX.getCause() instanceof com.ibm.websphere.ce.cm.ConnectionWaitTimeoutException)
          {
               
//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 for to share.
           }
          else
          {
               
// handle other database problems.
          }
          }
          finally {
               if (rs != null)
                    try {
                         rs.close();
                    }
                    catch (java.sql.SQLException sqle1) {
                    }
               if (stmt != null)
                    try {
                         stmt.close();
                    }
                    catch (java.sql.SQLException sqle1) {
                    }
               if (conn != null)
                    try {
                         conn.close();
                    }
                    catch (java.sql.SQLException sqle1) {
                    }
          }
     }





Related concepts


Data sources
Connection pooling

 

Related


Connection pool settings