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

此示例包含 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 事务语义。