Setting a TaskName using TaskNameAccessor API

You can use the TaskNameAccessor API to set a Java Persistence API (JPA) TaskName at runtime.

About this task

In the Enterprise JavaBeans (EJB) container, a task name is automatically set by default when a transaction begins. This action is performed when a component or business method is invoked in a CMT session bean or when an application invokes the sessionContext.getTransaction().begin() in a BMT session bean. This TaskName consists of a concatenation of the fully package qualified session bean type, a dot character and the method name, for example: com.acme.MyCmtSessionBean.methodABC.

If you are using JPA in the context of the Web container, an application must use the TaskNameAccessor API to set the TaskName in the current thread of execution.

Avoid trouble Avoid trouble: Once a TaskName is set on a transaction context, you cannot set the TaskName again in the same transaction. Following this rule avoids problems with the JDBC connection for different database access.gotcha
This example contains the TaskNameAccessor API definition
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 (for example,  in JSE environment).
	 */
	public static boolean setTaskName(String taskName);

}
This code example shows how to set a TaskName using TaskNameAccessor.
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 the caller 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
    }

}
Attention: In the previous example, an application must be aware that the transaction boundary is subtly changed if Ejb1 uses the local interface (@Local). For example, when caller_Method1() calls callee_Method3 or callee_LocalTx, this is a Java method call. No EJB transaction semantics are honored.

What to do next

Once you have completed this step, continue on with the topic, Specify TaskName in a JPA persistence unit.



In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Feb 5, 2014 9:49:51 PM CST
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=compass&product=was-nd-mp&topic=tejb_setTaskName
File name: tejb_setTaskName.html