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

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


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tdat_cusdatstore
文件名:tdat_cusdatstore.html