[Mybatis-spring] Transaction transaction/Transaction/transaction Manager

Source: Internet
Author: User

One of the main reasons for using mybatis-spring is:

Mybatis-spring allows MyBatis to participate in spring transactions.

Mybatis-spring Leverage[use (something) to maximum advantage)] The existence of Datasourcetransactionmanager,

Without intentionally creating a new transaction management for MyBatis.

Once spring Tansaction Manager is configured, you can configure transactions as you normally would in spring.

Two configuration modes are supported:

[Email protected] Comments

2.AOP style Configuration

During the transaction, a separate Sqlsession object is created and used.

When the transaction is complete, the session is either committed or rolled back.

Standard configuration

Creating the data source Transaction manager Datasourcetransactionmanager in the spring XML configuration file will allow spring to do transaction management.

<id= "TransactionManager"  class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">  <  name = "DataSource" ref = "DataSource" /> </ Bean >

The data source is configured by any JDBC datasource. (see Sqlsessionfactorybean's article).

This includes the connection pool and the data source (obtained by the Jndi query).

Note: The datasource used by the transaction manager must be consistent with the datasource of Sqlsessionfactorybean.

The following sections are not very useful to me at present, just by the way.

Container Managed Transactions

If you are using a JEE container and want spring to participate in container managed transactions (CMT),

Then spring should configure Jtatransactionmanager or one of its container-specific subclasses.

The easiest way to do this is to use the Spring transaction namespace:

</>

In this configuration, MyBatis will behave like other spring transaction sources configured by CMT.

Spring automatically uses any existing container transactions and formulates a sqlsession to it.

If no transaction is started, but one is required based on the transaction configuration, spring launches a new single CMT.

Note that if you use CMT but do not want to use spring transaction management, you cannot configure the Spring transaction manager,

And the Sqlsessionfactorybean must be configured using the MyBatis base managedtransactionfactory.

<BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">  < Propertyname= "DataSource"ref= "DataSource" />  < Propertyname= "Transactionfactory">    <Beanclass= "Org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />  </ Property>  </Bean>

With ↑ The part is of little use to me at present, just by the way translated.

Finally there is a piece: programmatic Transaction Management have time to engage.

[Mybatis-spring] Transaction transaction/Transaction/transaction Manager

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.