TaskNameAccessor API を使用した TaskName の設定

TaskNameAccessor API を使用して、実行時に Java™ Persistence API (JPA) TaskName を設定します。

このタスクについて

Enterprise JavaBeans (EJB) コンテナーでは、タスク名はトランザクションの開始時にデフォルトで自動的に設定されます。このアクションは、CMT セッション Bean でコンポーネントまたはビジネス・メソッドが呼び出されたとき、またはアプリケーションが BMT セッション Bean で sessionContext.getTransaction().begin() を呼び出したときに実行されます。 この TaskName は、完全パッケージ修飾セッション Bean タイプ、ドット文字、およびメソッド名を連結したものです。例えば、com.acme.MyCmtSessionBean.methodABC のようになります。

Web コンテナーのコンテキストで JPA を使用している場合は、アプリケーションで TaskNameAccessor API を使用して現在の実行スレッドに TaskName を設定する必要があります。

トラブルの回避 (Avoid trouble) トラブルの回避 (Avoid trouble): TaskName をトランザクション・コンテキストに一度設定すると、アプリケーションで同じトランザクションに再度 TaskName を設定することはできません。これにより、異なるデータベース・アクセスに対する JDBC 接続に関する問題が回避されます。gotcha
以下の例には TaskNameAccessor API 定義が含まれています。
package com.ibm.websphere.persistence;

public abstract class TaskNameAccessor {

	/**
	 	 * Returns the current task name attached in the current thread context.
	 	 * @return current task name or null if none is found.
	 */
	public static String getTaskName ();
	
	/**
	 	 * Add a user-defined JPA access intent task name to the current thread
	 	 * context.
	 * 
	 	 * @param taskName
	 	 * @return false if an existing task has already attached in the current
	 	 *         thread or Transaction Synchronization Registry (TSR) is not
	 	 *         available (i.e. in JSE environment).
	 */
		public static boolean setTaskName(String taskName);

}
このコード例は、TaskNameAccessor を使用して TaskName を設定する方法を示しています。
package my.company;

@Remote
class Ejb1 {
    // assumer no tx from the caller
    @TransactionAttribute(Requires)
    public void caller_Method1() {

        // an implicit new transaction begins
        // TaskName "my.company.Ejb1.caller_Method1" set on TSR

        ejb1.callee_Method?();
    }

    @TransactionAttribute(RequiredNew)
    public void callee_Method2() {

        // an implicit new transaction begins i.e. TxRequiredNew.
        // TaskName "my.company.Ejb1.callee_Method2" set on TSR
    }

    @TransactionAttribute(Requires)
    public void callee_Method3() {

        // In caller's transaction, hence TaskName remains 
        //     "my.company.Ejb1.caller_Method1"
    }

    @TransactionAttribute(NotSupported)
    public void callee_LocalTx () {

        // Unspecified transaction, a new local transaction implicitly started.
        // TaskName "my.company.Ejb1.callee_LocalTx" set on TSR
    }

}
重要: この例では、Ejb1 がローカル・インターフェース (@Local) を使用した場合、トランザクション境界がわずかに変わることを、アプリケーションが認識している必要があります。例えば、caller_Method1()callee_Method3 または callee_LocalTx を呼び出す場合、これは Java メソッド呼び出しとして扱われます。EJB トランザクション・セマンティクスが優先されることはありません。

次のタスク

このステップを完了したならば、引き続きトピック『JPA パーシスタンス・ユニットでの TaskName の指定』に進んでください。

トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tejb_setTaskName
ファイル名:tejb_setTaskName.html