Session EJB Series (iv) Sessionbean transaction management

Source: Internet
Author: User

(in the previous article we introduced the " session EJB Series (iii) Sessionbean life cycle ", this is the main introduction of Sessionbean transaction Management)

Sessionbean can be functionally divided into two categories:

First class: Become EAO: Used to encapsulate the underlying entities and JPA, which essentially functions as DAO. (transaction control is not required.) Because the JPA nature is the encapsulation of ORM implementations. )

Second class: Acting as a business component : The facade of the underlying EAO object, and system controllers (such as controllers in Action,springmvc in struts) call their business methods to handle user requests. (You need to add transaction control.) Because it represents a complete business logic)

The transaction management mechanism of EJBS is still based on JTA transactions, so the application server is responsible for handling the underlying details of transaction management.

Container Management transactions (CMT) and Bean Management transactions (BMT) are still available in EJB3 and allow the annotation mechanism to specify transaction properties, overriding the way XML configuration files are in EJB2.

For CMT: The container is responsible for determining the boundary of a transaction: The method begins with the transaction, and the end of the method is the end of the transaction.

For BMT: Transaction boundaries are flexibly determined by the developer (transaction start, transaction rollback, transaction end)

I. Container Management Services

CMT: The container is responsible for determining the boundary of a transaction: The method begins with the transaction, and the end of the transaction.

(1) Annotation Control service mode

Sessionbean's transaction management mechanism is exactly the same as the MDB's transaction management, and is controlled by using @transactionmanagement, @TransactionAttribute two annotation on the bean. There is no need to make any changes to the interface code.

1) @TransactionManagement, the Value property sets the transaction mode: CMT? Or a BMT? Such as:

Transactionmanagementtype.bean: Specify to use BMT to manage transactions

Transactionmanagementtype.container: Specifying the use of CMT to manage transactions

2) @TransactionAttribute, can be used to modify the Bean implementation class of Sessionbean or MDB, also to decorate a business method (priority),

and set the transaction property to one of the enumeration types through the value values:

(2) Capturing ' custom exception '

By default, a system exception occurs, and the CMT controls the rollback of the transaction, and if the custom exception is not rolled back.

However, you can manually control the ' custom exception ' rollback in the following way:

First: Use Ejbcontext's Setrollbackonly (), Show control code rollback

The second type: Use @applicationexception Annotation, specify the Rollback property to True

(3) Note

CMT means that the container manages the transactions in the EJB business method, so the developer should not invoke any method of starting or ending the transaction at development time.

Two. Bean Management

for BMT: Transaction boundaries are flexibly determined by the developer (transaction start, transaction rollback, transaction end), primarily through usertransaction to control transactions. The essence is to programmatically manipulate the JTA transaction manager.

(1) There are 3 ways to get the UserTransaction object in the Bean class of the EJB:

First: Use @resource annotation to perform dependency injection

The second type: lookup through Jndi, such as: UserTransaction ut= (usertransaction) ctx.lookup ("UserTransaction");

The Third Kind: Use Ejbcontext's Getusertransaction () method to obtain, such as: UserTransaction ut=ctx.getusertransaction ();

(2) Simple analysis of the methods contained in UserTransaction (slightly)

Three. Compare CMT and BMT

(1) Features

CMT (default): A container is responsible for determining the boundaries of a transaction: The beginning of the method is the beginning of the transaction, and the end of the transaction.

BMT: Transaction boundaries are flexibly determined by the developer (transaction start, transaction rollback, transaction end). Essentially, the JTA transaction manager is programmed to operate.

Typically, the EJB container recommends the use of CMT transaction management.

(2) Advantages and disadvantages

BMT Advantages: More flexible! Developers decide on their own transaction boundaries

BMT Disadvantages:

1. Because developers use hard-coded methods to implement transaction management, it is inevitable that the business logic and the lack of logic confusion, it is difficult to switch other management methods.

2. When the client invokes the BMT method, the current transaction is paused, which restricts the reuse of the component

(3) BMT has two types of scenarios that must exist, as well as the corresponding controversy:

1) The developer needs to add a transaction for a piece of code in the business method. (Why not pull this code away?) Encapsulated as a method? )

2) stateful Sessionbean need to maintain transactions across method invocations, that is, multiple methods need to be placed in one transaction for maintenance. (Show multiple methods that make up a complete business logic, why not encapsulate them as a single business method)

Four. Summary

There are two types of transaction management in Sessionbean: Container management Transaction (CMT) and Bean Management Transaction (BMT)

CMT (default): A container is responsible for determining the boundaries of a transaction: The beginning of the method is the beginning of the transaction, and the end of the transaction.

BMT: Transaction boundaries are flexibly determined by the developer (transaction start, transaction rollback, transaction end). Essentially, the JTA transaction manager is programmed to operate.

Specific operation of the specific situation ...

Session EJB Series (iv) Sessionbean transaction management

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.