Jakarta TransactionsThe Jakarta Transactions (JTA; formerly Java Transaction API), one of the Jakarta EE APIs, enables distributed transactions to be done across multiple X/Open XA resources in a Java environment. JTA was a specification developed under the Java Community Process as JSR 907. JTA provides for:
X/Open XA architectureIn the X/Open XA architecture, a transaction manager or transaction processing monitor (TP monitor) coordinates the transactions across multiple resources such as databases and message queues. Each resource has its own resource manager. The resource manager typically has its own API for manipulating the resource, for example the JDBC API to work with relational databases. In addition, the resource manager allows a TP monitor to coordinate a distributed transaction between its own and other resource managers. Finally, there is the application which communicates with the TP monitor to begin, commit or roll back the transactions. The application also communicates with the individual resources using their own API to modify the resource. JTA implementation of the X/Open XA architectureThe JTA API consists of classes in two Java packages: The JTA is modelled on the X/Open XA architecture, but it defines two different APIs for demarcating transaction boundaries. It distinguishes between an application server such as an EJB server and an application component. It provides an interface, The JTA architecture requires that each resource manager must implement the
APIThe Jakarta Transactions API consists of three elements: a high-level application transaction demarcation interface, a high-level transaction manager interface intended for an application server, and a standard Java mapping of the X/Open XA protocol intended for a transactional resource manager. UserTransaction interfaceThe The Support for nested transactions is not required. The UserTransaction.begin method throws the NotSupportedException when the calling thread is already associated with a transaction and the transaction manager implementation does not support nested transactions. Transaction context propagation between application programs is provided by the underlying transaction manager implementations on the client and server machines. The transaction context format used for propagation is protocol dependent and must be negotiated between the client and server hosts. For example, if the transaction manager is an implementation of the JTS specification, it will use the transaction context propagation format as specified in the CORBA OTS 1.1 specification. Transaction propagation is transparent to application programs. @Transactional annotationThe The code sample below illustrates the usage of @RequestScoped
public class ExampleBean {
@Transactional
public void foo() { // A transaction is active here
// Do work
} // After the method returns transaction is committed or rolled back
}
Transactional behavior can be configured via an attribute on the annotation. The available options closely mirror those of the EJB specification. @TransactionScoped annotationThe The code sample below illustrates the usage of @TransactionScoped
public class TxScopedBean {
public int number;
public int getNumber() {return number;}
public void setNumber(int number) {this.number = number;}
}
@RequestScoped
public class ExampleBean {
@Inject
private TxScopedBean txScopedBean;
@Transactional
public void foo() {
txScopedBean.setNumber(1);
}
@Transactional
public void bar() {
System.out.print(tXscopedBean.getNumber());
}
}
If method foo() is first called on a managed instance of ExampleBean and then subsequently method bar() is called, the number printed will be 0 and not 1. This is because each method had its own transaction and therefore its own instance of TxScopedBean. The number 1 that was set during the call to foo() will therefore not be seen during the call to bar(). UserTransaction support in EJB serverEJB servers are required to support the The code sample below illustrates the usage of @Stateless
@TransactionManagement(BEAN)
public class ExampleBean {
@Resource
private UserTransaction utx;
public void foo() {
// start a transaction
utx.begin();
// Do work
// Commit it
utx.commit();
}
}
Alternatively, the @Stateless
@TransactionManagement(BEAN)
public class ExampleBean {
@Resource
private SessionContext ctx;
public void foo() {
UserTransaction utx = ctx.getUserTransaction();
// start a transaction
utx.begin();
// Do work
// Commit it
utx.commit();
}
}
Note though that in the example above if the UserTransaction support in JNDIThe UserTransaction should be available under See alsoReferencesExternal links |