Operating Systems: i5/OS
             Personalize the table of contents and search results

 

Develop a custom DataStoreHelper class

 

Apply the WebSphere extension, GenericDataStoreHelper class, to create your own data store helper for data sources that the Application Server does not support. With this helper class, your JDBC configuration can use database-specific functions during transactions. If you are using a configuration with a data source that is not supported by the Application Server, you might want to create a custom data store helper. This helper will allow you to leverage the database to perform functions during a transaction that would not otherwise be available. You will need to create a user-defined DataStoreHelper class, and there is information for creating a new exception handler to catch any exceptions that might be created with the use of your custom data handler.

 

Procedure

  1. Create a class that extends the com.ibm.websphere.rsadapter.GenericDataStoreHelper.java class. Use the following code as an example (this type of data source is based on a user-defined JDBC provider):

    package com.ibm.websphere.examples.adapter;
    
    import java.sql.SQLException;
    import javax.resource.ResourceException;
    
    import com.ibm.websphere.appprofile.accessintent.AccessIntent;
    import com.ibm.websphere.ce.cm.*;
    import com.ibm.websphere.rsadapter.WSInteractionSpec;
    
    /**
    * Example DataStoreHelper class, demonstrating how to create a user-defined DataStoreHelper.
    * Implementation for each method is provided only as an example.  More detail would likely be
    * required for any custom DataStoreHelper created for use by a real application.
    * In this example, we will override the doStatementCleanup(),getIsolationLevel(), and set userDefined 
    * exception map
    */
    public class ExampleDataStoreHelper extends com.ibm.websphere.rsadapter.GenericDataStoreHelper
    {
    
        public ExampleDataStoreHelper(java.util.Properties props)
        {
            super(props);
    
            // Update the DataStoreHelperMetaData values for this helper.
            getMetaData().setGetTypeMapSupport(false);
    
            // Update the exception mappings for this helper.
            java.util.Map xMap = new java.util.HashMap();
    
            // Add an Error Code mapping to StaleConnectionException.
            xMap.put(new Integer(2310),  StaleConnectionException.class);
            // Add an Error Code mapping to DuplicateKeyException.
            xMap.put(new Integer(1062),  DuplicateKeyException.class);
            // Add a SQL State mapping to the user-defined ColumnNotFoundException
            xMap.put("S0022",            ColumnNotFoundException.class);
            // Undo an inherited StaleConnection SQL State mapping.
            xMap.put("S1000",            Void.class);
    
            setUserDefinedMap(xMap);
    
            // If you are extending a helper class, it is
            // normally not necessary to issue 'getMetaData().setHelperType(...)'
            // because your custom helper will inherit the helper type from its
            //  parent class.
                              
    
             }
    
        public void doStatementCleanup(java.sql.PreparedStatement stmt) throws SQLException
        {
            // Clean up the statement so it may be cached and reused.
    
            stmt.setCursorName("");
            stmt.setEscapeProcessing(true);
            stmt.setFetchDirection(java.sql.ResultSet.FETCH_FORWARD);
            stmt.setMaxFieldSize(0);
            stmt.setMaxRows(0);
            stmt.setQueryTimeout(0);
        }
    
    
    
        public int getIsolationLevel(AccessIntent intent) throws ResourceException
        {
            // Determine an isolation level based on the AccessIntent.
    
            // set WebSphere default isolation level to TRANSACTION_SERIALIZABLE.
            if (intent == null) return java.sql.Connection.TRANSACTION_SERIALIZABLE;
    
            return intent.getConcurrencyControl() == AccessIntent.CONCURRENCY_CONTROL_OPTIMISTIC ?
                   java.sql.Connection.TRANSACTION_READ_COMMITTED :
                   java.sql.Connection.TRANSACTION_REPEATABLE_READ;
        }
            public int getLockType(AccessIntent intent) {
               if ( intent.getConcurrencyControl() == AccessIntent.CONCURRENCY_CONTROL_PESSIMISTIC) {
                  if ( intent.getAccessType() == AccessIntent.ACCESS_TYPE_READ ) {
                      return WSInteractionSpec.LOCKTYPE_SELECT;
                  }
                  else {
                      return WSInteractionSpec.LOCKTYPE_SELECT_FOR_UPDATE;
                   }
               }
               return WSInteractionSpec.LOCKTYPE_SELECT;
            }
    }
    

  2. Optional: Create your own exception handler class. Use the following code as a guide:

    package com.ibm.websphere.examples.adapter;
    
    import java.sql.SQLException;
    import com.ibm.websphere.ce.cm.PortableSQLException;
    
    /**
    * Example PortableSQLException subclass, which demonstrates how to create a user-defined
    * exception for exception mapping.
    */
    public class ColumnNotFoundException extends PortableSQLException
    {
        public ColumnNotFoundException(SQLException sqlX)
        {
            super(sqlX);
        }
    }
    

  3. Compile the newly created DataStoreHelper class or classes. You will need the following JAR files in your classpath to compile them:

  4. Place your compiled JAR files in the directory app_server_root\lib\ext. This directory might not exist by default, so you might need to create the ext directory in the app_server_root\lib directory.

  5. Configure the Application Server to use your new class.

    Note: If you only need to add exception handlers, you do not need to configure the Application Server to use them.

    1. From the administrative console select Resources > JDBC > Data Sources.

    2. If there are no data sources in the list, change the scope to All Scopes.

    3. Select the default data source.

    4. Name your data source in the Name field.

    5. In the section labeled Data store helper class name, select Specify a user-defined data store helper.

    6. Enter the class name for the data store helper that you created.

    7. Apply your changes and select Ok.




}
Resource adapters