开发定制 DataStoreHelper 类

应用 WebSphere® 扩展 GenericDataStoreHelper 类,以便为应用程序服务器不支持的数据源创建您自己的数据存储辅助控件。借助此 helper 类,事务期间 JDBC 配置可以使用特定于数据库的函数。

开始之前

如果要将配置与应用程序服务器不支持的数据源配合使用,那么可能要创建定制数据存储辅助控件。此辅助控件允许您在执行本来可用的事务期间利用数据库来执行功能。您需要创建用户定义的 DataStoreHelper 类,其中包括用于创建新的异常处理程序以捕获使用定制数据处理程序时形成的异常的信息。

关于此任务

不推荐使用的功能部件 不推荐使用的功能部件: 建议您不要使用 com.ibm.websphere.rsadapter.DataStoreHelper 类 API 中的 CUSTOM_HELPER 常量字段。如果创建了自己的 DataStoreHelper 实现类,那么不要调用 setHelperType(DataStoreHelper.CUSTOM_HELPER)。改为由它继承的实现 类来设置 HelperType 值。depfeat

过程

  1. 创建用于扩展现有数据存储辅助控件的类。 将以下代码用作示例;此类型的数据源基于用户定义的 JDBC 提供程序:
    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.
    * The implementation for each method is provided only as an example.  More detail is probably
    * required for any custom DataStoreHelper that is 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. 可选: 创建您自己的异常处理程序类。 请将下列代码用作指南:
    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. 编译新创建的 DataStoreHelper 类。 您需要将下列 JAR 文件包括在类路径中以编译它们:
    • 如果要使用 Eclipse 之类的开发环境,那么需要在类路径中设置这些 JAR 文件才能进行编译。然后,在编辑完文件后创建项目的 JAR 文件(请参阅开发环境的帮助文档以了解特定指示信息)。
    • 如果没有开发环境,并且在使用 javac 编译器:
      1. 创建用于扩展 GenericDataStoreHelper 的 .java 文件,如步骤 1 中所示。
      2. 在命令行实用程序中编辑完文件后切换至主目录。
      3. 使用以下命令设置类路径:
        [Windows]
        set CLASSPATH=%CLASSPATH%;app_server_root\dev\JavaEE\j2ee.jar;app_server_root\dev\was_public.jar;app_server_root\plugins\com.ibm.ws.runtime.jar
        [AIX][HP-UX][Linux][Solaris]
         set CLASSPATH=$CLASSPATH:app_server_root/dev/JavaEE/j2ee.jar:app_server_root/dev/was_public.jar:app_server_root/plugins/com.ibm.ws.runtime.jar
      4. 编译类。例如,在 Windows 操作系统上输入以下命令(这会编译您指定的目录中的所有 .java 文件):
        C:\javac your_directory\*.java
      5. Java 目录中,为目录中的所有已编译类文件创建 JAR 文件。例如,在 Windows 操作系统上输入以下命令(将 myFile 更改为要对 JAR 文件使用的名称):
         C:\Java> jar cf myFile.jar *.class
        有关使用 javac 编译器的更多信息,请参阅有关 Java™ 编译器的 Oracle Web 站点。
  4. 将已编译的 JAR 文件放入目录中,然后更新 JDBC 提供程序的类路径以包含该位置。 例如,如果 JAR 文件是 c:\myFile.jar,请确保修改 JDBC 类路径以包含 c:\myFile.jar。
    1. 单击资源 > JDBC > JDBC 提供程序 > JDBC_provider
    2. 类路径字段中,添加您所编译的 JAR 文件的位置。 例如,在字段中按 ENTER 键,然后添加新行:
      c:\myFile.jar
  5. 将应用程序服务器配置为使用新的定制 DataStoreHelper 类。
    1. 在管理控制台中,选择资源 > JDBC > 数据源
    2. 选择要使用定制 DataStoreHelper 类来配置的数据源。
    3. 在标签为数据存储 helper 类名的部分中,选择指定用户定义的数据存储辅助控件
    4. 输入您创建的数据存储辅助控件的类名。
    5. 应用更改并选择确定

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tdat_cusdatstore
文件名:tdat_cusdatstore.html