spring-Transaction Configuration and explanation

Source: Internet
Author: User

One, spring transaction configuration

Transactions are often used during project development to ensure data consistency. Sort out some of the ways you configure transactions in spring, based on the information on the web. Either way, you need to configure the connection pool and transaction manager in the configuration file, as shown in the code below.

<!--reading configuration Files -<Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     < Propertyname= "Locations">         <List>             <value>Classpath:database.properties</value>             <value>Classpath:service.properties</value>         </List>     </ Property>     < Propertyname= "fileencoding"value= "UTF-8" />     < Propertyname= "Ignoreresourcenotfound"value= "false" /></Bean><!--Connection Pool -<BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">    < Propertyname= "Driverclassname"value= "${db.driver}" />    < Propertyname= "url"value= "${db.url}" />    < Propertyname= "username"value= "${db.username}" />    < Propertyname= "Password"value= "${db.password}" /></Bean><!--Configure transaction manager -<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">    < Propertyname= "DataSource"ref= "DataSource" /></Bean>

1. Declarative transaction Management

  1.1 configuration based on ASPECTJ XML This is the best way I think, based on AOP configuration, there is no need to modify code when new methods are used for transaction management. First introduce the AOP and TX namespaces in the configuration file XML

  

xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: Schemalocation= "http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/ SPRING-TX-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP   http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd "

1.2 then adds the AOP configuration to the XML. The following configuration is to apply the Txadvice enhancement to a services pointcut, where the pointcut for services is the enhancement of applying txadvice in all methods under the Ymy.com.service.impl package. The Txadvice then adds transaction management to all the methods that begin with Create,add,delete,update,change.

<!--defining transaction notifications (transaction enhancements) -<Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager">    <!--define filter rules for methods -    <tx:attributes>        <!--All methods use transactions -        <!--Propagation: Transaction propagation behavior Isolation: Transaction isolation read-only: Read-only rollback-for: What exception rollback occurred No-rollback-for: What exceptions do not roll back timeout: expiration information -        <Tx:methodname= "create*"Propagation= "REQUIRED"/>        <Tx:methodname= "add*"Propagation= "REQUIRED"/>        <Tx:methodname= "delete*"Propagation= "REQUIRED"/>        <Tx:methodname= "update*"Propagation= "REQUIRED"/>        <Tx:methodname= "change*"Propagation= "REQUIRED"/>    </tx:attributes></Tx:advice><!--defining an AOP configuration configuration slice -<Aop:config>    <!--Define a pointcut -    <Aop:pointcutID= "Services"expression= "Execution (* ymy.com.service.impl.*.* (..))" />    <!--to adapt the notification of pointcuts and transactions -    <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Services"/></Aop:config>

Configured in this way, transaction management is added when the method is named according to the rules defined by the transaction.

2. Annotation-based transaction management

This way is the simplest I think, the second recommendation. To use annotations, you need to open the annotation transaction in the configuration file.

<!---<transaction-manager= "TransactionManager "/>

Only add annotations on the corresponding classes when using @transactional.

@Service @transactional (propagation=Propagation.REQUIRED)
 Public class Implements Itaskservice {}
Ii. Spring transaction 1. The isolation level of database transactions has four READ UNCOMMITTED (read UNCOMMITTED), Read committed (Read Committed), repeatable read (repeat Read)MySQL Database default isolation level, Serializable (serialized)default Isolation level for Oracle databases, these four levels can solve the problems of dirty reading, non-repeatable reading, and Phantom reading one after the other. Dirty read:Dirty reads refer to the data in another uncommitted transaction that is read in one transaction process non-repeatable READ:Refers to a data that is queried several times under a transaction, and the results are different. This is because the query interval is modified by another transaction, such as transaction T1 query a data, transaction T2 modified the data, T1 and query once again, the result is different from the first time, there is a non-repeatable degree Phantom read:Phantom reading is a phenomenon that occurs when a transaction is not executed independently. For example, a transaction T1 a data item from "1" to "2" for all rows in a table, and the transaction T2 inserts a row of data items into the table, and the value of this data item is "1" and is submitted to the database.                                                  The user of the operation transaction T1, if you look at the data you just modified, will find that there is another line that has not been modified, in fact, this line is added from the transaction T2, as if the illusion, this is the occurrence of Phantom read. √: May appear x: does not appear 2, spring transactions have 7 propagation behavior

                              

3. The isolation level of spring transactions

                              

4. Note of spring transaction rollback 4.1 Principles and concepts:@Transaction is based on the dynamic agent mechanism a. @Transaction applied to the public methods in interfaces, interface methods, classes, classes, and B. @Transaction can only be applied to methods defined by public, if used in protected or It does not produce the effect of a transaction. C. It is recommended that @transaction be declared on the method in the class or class, not on the interface and interface methods.    Use @Transactional annotations on an interface only if you set up an interface-based proxy. D. A method in a class calls a B method with a transaction, and the transaction does not work. E. Default spring transactions are rolled back only when an uncaught runtimeexcetpion occurs This situation does not roll back
               These two situations will roll back          

spring-Transaction Configuration and explanation

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.