Transaction __java in Java EE

Source: Internet
Author: User
Tags rollback

A transaction is a series of actions that must take effect at the same time or cancel execution at the same time. All operations in a transaction take effect as a commit of a transaction, and all operations in the transaction cancel the rollback that is called a transaction. The end of a transaction can only be committed or rolled back in two cases. Transactions have an acid four attribute.

Java EE provides the following two kinds of management mechanisms for transactions:

Container-managed transactions CMT Bean-managed transactions (also known as application-managed transactions) BMT

1. CMT

CMT is a declarative transaction and is not intrusive to the application code. If an EJB is declared as CMT, then the EJB container is responsible for the transaction management of the EJB, that is, the attributes of the transaction are transparent to the EJB, and there is no transaction-related code in the EJB class. In fact, the bottom of the CMT implementation is also dependent on the JTA transaction API.

For the CMT EJB, the execution of one business method for the EJB corresponds to one transaction. Execution begins a transaction as soon as it enters the EJB business method, and automatically commits or rolls back the transaction when the EJB business method has finished executing.

CMT does not support nested or multiple transactions, that is, when executing an EJB business method, it is impossible to have two or more transactions executing at the same time, and no other transactions can be included in the execution of the transaction. However, you can pause a transaction and then perform another transaction, and then execute the first transaction when the second transaction completes.

1 Statement of the transaction

In the Jeb of CMT, all business methods that are not EJBS support transactions, which determines how transactions are declared in the EJB. Java EE provides @javax. Ejb.transactionattribute annotations for declaring transactions. If the annotation acts on the EJB class, the business methods in the entire EJB class support the transaction. The annotation can also function only on certain business methods in the EJB class, the business method that uses the callout supports the transaction, executes in the transaction, and the business method that does not use the callout does not execute in the transaction.

@javax.ejb. The transactionattribute callout can take a JAVAX.EJBWhen declaring a transaction. The enumeration parameters for the transactionattributetype type are as follows, specifying the transactions during the execution of the business method:

Javax.ejb.TransactionAttributeType.Required (default)

If the business method is already in a transaction before it is invoked, the execution of the business method is added to the current transaction;

If the business method is not in a transaction before it is invoked, the transaction is created and the execution of that business method is added to the transaction

Javax.ejb.TransactionAttributeType.RequiresNew

If the business method is already in a transaction before it is invoked, the current transaction is suspended, the new transaction is created and the execution of the business method is added to the new transaction, the business method finishes, and the pending transaction is restored.

If the business method is not in a transaction before it is invoked, the transaction is created and the execution of that business method is added to the transaction

Javax.ejb.TransactionAttributeType.Mandatory

If the business method is already in a transaction before it is invoked, the execution of the business method is added to the current transaction;

If the business method is not in the transaction before it is invoked, the EJB container throws Transactionrequiredexception

Javax.ejb.TransactionAttributeType.Never

If the business method is already in a transaction before it is invoked, the EJB container throws RemoteException

If the business method is not in the transaction before it is invoked, the business method is continued to be performed outside the transaction

javax.ejb.TransactionAttributeType.NotSupported

If the business method is already in a transaction before it is invoked, the current transaction is suspended, the business method is executed outside of the transaction, and the business method is completed before the suspended transaction is resumed

If the business method is not in the transaction before it is invoked, the business method is continued to be performed outside the transaction

Javax.ejb.TransactionAttributeType.Supports

If the business method is already in a transaction before it is invoked, the business method is executed within the transaction

If the business method is not in the transaction before it is invoked, the business method is continued to be performed outside the transaction


An example of an EJB class transaction sound is as follows:

@javax. Ejb.transactionattribute (Javax.ejb.TransactionAttributeType.NOT_SUPPORTED)
@Stateful
Public Class Transactionbean implements Transaction {
...
    @ javax.ejb.TransactionAttribute (Javax.ejb.TransactionAttributeType.REQUIRES_NEW) public
    void FirstMethod () { ...}


2 Rollback of transaction

The CMT transaction rollback has the following 2 scenarios:

In the process of executing EJB business method, the system exception EJB business method has been used to invoke the Setrollbackonly () method of the Javax.ejb.EJBContext interface actively

3 The limitation of CMT transaction to EJB Business method

The EJB business approach using CMT must not contain the following transaction-related statements:

A commit (), Setautocommit (), rollback () method in any method in the Javax.transaction.UserTransaction interface java.sql.Connection The Getusertransaction () method in the Javax.jms.Session (), Rollback () method Javax.ejb.EJBContext

2. BMT

Although the CMT is easy to implement programmatically, the logic in the transaction is simpler and does not support nested transactions. BMT can implement complex transaction-related logic.

BMT, also known as application-managed transactions, can be grouped into the following 2 categories:

JDBC Transaction JTA Transaction

JDBC transactions are not under the control of the Java EE transaction manager, so multiple JDBC transactions can be manipulated in a Java EE transaction.

JTA provides a form that is independent of the specific implementation of a transaction for the application of access to transactions, that is, the implementation of the transaction and the access to the transaction.


JTA defines a standard Java access interface between the Java EE transaction manager and the application that participates in the transaction. JTA transactions involve the following 3 roles: application, the call in the Javax.transaction.UserTransaction interface in the business method (), Commit (), the rollback () method Java EE server, Communication between application and transaction manager Java EE transaction manager, controlling access to resources (through interaction with the resource manager)

JTA transactions are controlled by the Java EE transaction manager, JTA transactions can support access to a variety of different resources, such as databases, but do not support nested transactions.
BMT must be a matter of beginning and finish. In an applied transaction method, only one transaction can be opened through the Begin () method in the Javax.transaction.UserTransaction interface. Ending BMT transactions can be divided into different situations. For stateless session beans, the transaction method must call the commit () or rollback () method in the Javax.transaction.UserTransaction interface before returning. For stateful session beans, if it is a JDBC transaction, it runs in the transaction as long as the JDBC connection exists until the JDBC connection is closed; if it is a JTA transaction, The transaction is closed until the commit () or rollback () method in the Javax.transaction.UserTransaction interface is invoked, regardless of whether the JDBC connection during the period is closed.


In a BMT EJB business approach, the getrollbackonly () or Setrollbackonly () method of the Javax.ejb.EJBContext interface must not be invoked.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.