カスタム DataStoreHelper クラスの開発

WebSphere® 拡張機能 GenericDataStoreHelper クラスを適用して、アプリケーション・サーバーがサポートしないデータ・ソース用の独自のデータ・ストア・ヘルパーを作成します。 このヘルパー・クラスにより、JDBC 構成は、トランザクション中にデータベース固有の機能を使用できます。

事前処理

アプリケーション・サーバーでサポートされないデータ・ソースを持つ構成を使用する場合、 カスタム・データ・ストア・ヘルパーを作成することがあります。 このヘルパーを使用すると、データベースを活用して、トランザクション中に、それ以外の場合には利用可能にならない機能を実行できます。 ユーザー定義 DataStoreHelper クラスを定義する必要があります。そのクラスには、カスタム・データ・ハンドラーの利用に伴い発生する可能性のあるすべての例外をキャッチする、新規の例外ハンドラーを作成するための情報があります。

このタスクの概要

非推奨フィーチャー: com.ibm.websphere.rsadapter.DataStoreHelper クラス API の CUSTOM_HELPER 定数フィールドは非推奨です。 独自の DataStoreHelper 実装クラスを作成する場合は、 setHelperType(DataStoreHelper.CUSTOM_HELPER) を呼び出さないでください。 その代わりに、 継承元の実装クラスによって HelperType 値が設定されるようにします。

手順

  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 クラスを 1 つまたは複数コンパイルします。
    これらをコンパイルするには、クラスパスに以下の JAR ファイルが必要です。
    • app_server_root/dev/JavaEE/j2ee.jar
    • app_server_root/dev/was_public.jar
      注: was_public.jar には、カスタム DataStoreヘルパー・クラスをコンパイルするために必要な app_server_root/lib/rsahelpers.jar のクラスが含まれています。
    • app_server_root/plugins/com.ibm.ws.runtime.jar
    • Eclipse のような開発環境を使用している場合、コンパイルを行えるようにするには、前述の JAR ファイルをクラスパスに設定する必要があります。 ファイルの編集が終了したら、プロジェクトの JAR ファイルを作成します (個々の指示については、ご使用の開発環境のヘルプ文書を参照してください)。
    • 開発環境を持たず、javac コンパイラーを使用している場合の手順は、次のとおりです。
      1. ステップ 1に示すように、 GenericDataStoreHelper またはその他のデータ・ストア・ヘルパーを拡張する .java ファイルを作成します。
      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
        
        [Linux][AIX][HP-UX][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. クラスを 1 つまたは複数コンパイルします。 例えば、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. 「データ・ストア・ヘルパー・クラス名」というラベルのセクションで、 「ユーザー定義データ・ストア・ヘルパーの指定」を選択します。
    4. 作成したデータ・ストア・ヘルパーのクラス名を入力します。
    5. 変更を適用し、 「OK」を選択します。