Comprehensive Analysis of EJB programmatic Transaction Management (BMT) and declarative Transaction Management (CMT) (II) -- declarative transactions

Source: Internet
Author: User

6. EJB declarative Transaction Management (CMT, container management transaction)

The EJB container provides a default transaction mode for all the EJB components: requires

In this mode, if the method in the component is called in the transaction environment (that is, the client calls the method of the component), the logic in the method will be added to the transaction environment of the client, use the same transaction control object as the client program. if it is not called in the transaction environment (the method in this component is not called on the client), The EJB container creates a new transaction object and manages all the logic in this method.

For example:

EJB components:

@ Statefull

Public classpersonmanager inplements ipersonadmin {

@ Persistencecontext (unitname = "mydb ")

Privateentitymanager manager;

@ EJB (mappedname = "mysession/remote ")

Privateipersonadmin mytools;

Publiclist <person> queryall (){

....

}

Public voidcreateperson (person p ){

Person pobj = newperson ();

Pobj. setname ("ziaoxioa ");

# 1manager. persist (pobj );

# 2mytools. createperson (P );

# 3manager. persist (null );

}

Customer Code:

...

Myinf. createperson (P );

...

By default, the EJB container configures a requires transaction service for all methods in the EJB component. in this transaction mode, if a method is called in the transaction environment, the method uses the client transaction object and the transaction environment ruguo, which is not called in the transaction environment, the EJB container will create a new transaction object and environment to manage the logic integrity of this method! Due to the #3 error above, no records will be added to the database!

The default transaction policy that the EJB container adds to the EJB component can meet the algorithm requirements in most cases. In special cases, you can mark @ transactionattribute and <container-transaction> to modify the default transaction policy of a component! As follows:

<EJB-jar

Xmlns = "http://java.sun.com/xml/ns/javaee"

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee

Http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"

Version = "3.0">

<Assembly-Descriptor>

<Container-transaction>

<Method>

<EJB-Name> mysession </EJB-Name>

<Method-Name> createperson </method-Name>

</Method>

<Trans-attribute> requiresnew </trans-attribute>

</Container-transaction>

</Assembly-Descriptor>

</EJB-jar>

@ Transactionattribute (requires_new)

Public voidcreateperson (person p ){

Manager. persist (P );

}

In this way, the createperson method transaction in mysession is changed to the requiesnew mode. In this mode, the EJB container creates a new transaction object for createperson regardless of whether the client has transaction characteristics, at this time, the two createperson instances are under completely different transaction object control.

 

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.