Jakarta Transactions

Finally, there is the application which communicates with the TP monitor to begin, commit or roll back the transactions.

It provides an interface, javax.transaction.TransactionManager, that is used by the application server itself to begin, commit and roll back the transactions.

It provides a different interface, the javax.transaction.UserTransaction, that is used by general client code such as a servlet or an EJB to manage the transactions.

The transaction context format used for propagation is protocol dependent and must be negotiated between the client and server hosts.

The javax.transaction.TransactionScoped annotation provides the application the ability to declare that the scope during which a bean lives is tied to the time a given transaction is active.

The code sample below illustrates the usage of @TransactionScoped in a request scoped CDI managed bean: 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.

[1]) The code sample below illustrates the usage of UserTransaction via bean-managed transactions in an EJB session bean: Alternatively, the UserTransaction can be obtained from the SessionContext: Note though that in the example above if the @TransactionManagement(BEAN) annotation is omitted, a JTA transaction is automatically started whenever foo() is called and is automatically committed or rolled back when foo() is exited.