使用 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。

避免故障 避免故障: 对事务上下文设置 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_Method3callee_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