--xml configuration implementation using AOP for transaction management in Spring MVC

Source: Internet
Author: User
Tags aop

1. Write an example of transaction management using AOP today, and the following are the first things you need to know about transactions

(1) Characteristics of the transaction

    • Atomicity (atomicity): A transaction is an atomic operation that consists of a series of actions. The atomicity of a transaction ensures that the action is either complete or completely ineffective.
    • Consistency (consistency): Once a transaction is complete (whether successful or unsuccessful), the system must ensure that the business it is modeling is in a consistent state, not partially completed partial failure. In reality the data should not be destroyed.
    • Isolation (Isolation): There may be many transactions that process the same data at the same time, so each transaction should be isolated from other transactions to prevent data corruption.
    • Persistence (Durability): Once a transaction is complete, no matter what system error occurs, its results should not be affected so that it can recover from any system crashes. Typically, the result of a transaction is written to persistent memory.

(2) Communication behavior of the transaction

The first aspect of a transaction is the propagation behavior (propagation behavior). When a transaction method is called by another transaction method, you must specify how the transaction should propagate.

propagation Behavior meaning
Propagation_required Indicates that the current method must be running in a transaction. If the current transaction exists, the method will run in that transaction. Otherwise, a new transaction is started
Propagation_supports Indicates that the current method does not require a transaction context, but if there is a current transaction, the method runs in this transaction
Propagation_mandatory Indicates that the method must run in a transaction and throws an exception if the current transaction does not exist
Propagation_required_new Indicates that the current method must be running in its own transaction. A new transaction will be started. If there is a current transaction, the current transaction is suspended during the execution of the method. If you use Jtatransactionmanager, you need to access TransactionManager
propagation_not_supported Indicates that the method should not run in a transaction. If there is a current transaction, the current transaction will be suspended while the method is running. If you use Jtatransactionmanager, you need to access TransactionManager
Propagation_never Indicates that the current method should not run in the transaction context. If a transaction is currently running, an exception is thrown
propagation_nested Indicates that if a transaction already exists, the method will run in a nested transaction. Nested transactions can be individually committed or rolled back independently of the current transaction. If the current transaction does not exist, then its behavior is the same as propagation_required. Note that each vendor's support for this kind of communication behavior is different. You can refer to the Resource Manager's documentation to confirm that they support nested transactions

(3) Isolation level of the transaction

The second dimension of a transaction is the isolation level (isolation levels). The isolation level defines the extent to which a transaction may be affected by other concurrent transactions

    • Dirty Read (Dirty reads)--dirty reads occur when one transaction reads data that another transaction overwrites but has not yet committed. If the overwrite is rolled back later, the data obtained by the first transaction is invalid.
    • Non-repeatable read (nonrepeatable Read)--non-repeatable reads occur when a transaction executes the same query two or more times, but each time a different data is obtained. This is usually because another concurrent transaction was updated during two queries.
    • Phantom Read (Phantom Read)--The Phantom read is similar to non-repeatable reading. It occurs when a transaction (T1) reads a few rows of data, and then another concurrent transaction (T2) inserts some data. In the subsequent query, the first transaction (T1) will find a number of records that do not already exist.
Isolation Level meaning
Isolation_default Using the default isolation level of the back-end Database
isolation_read_uncommitted Lowest isolation level, allowing reading of data changes that have not yet been committed and may result in dirty reads, Phantom reads, or non-repeatable reads
isolation_read_committed Allows reading of data that has been committed by a concurrent transaction to prevent dirty reads, but phantom or non-repeatable reads can still occur
Isolation_repeatable_read Multiple read results for the same field are consistent unless the data is modified by itself and can prevent dirty reads and non-repeatable reads, but Phantom reads can still occur
Isolation_serializable The highest isolation level, fully compliant with the ACID isolation level, ensures that dirty reads, non-repeatable reads, and Phantom reads are blocked, as well as the slowest transaction isolation level, because it is typically implemented by fully locking transaction-related database tables

2. Transaction management has programmatic transaction management and declarative transaction management. AOP is declarative transaction management, there are two ways to implement transaction management using AOP, one is XML configuration and one is annotation. The recommended XML profile, because it is convenient, once configured to take effect on all methods under the corresponding package, no longer annotate each method. But annotated use is more flexible.

The implementation only needs to be configured appropriately in the spring configuration file. Note The header file adds support accordingly.

This is a header file.

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd          Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd Http://www.springframework.org/schema/jee Http://www.springframework.org/schema/jee/sprin G-jee-4.0.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx-4.0.xs D ">           

This is a transaction management-related configuration

<!--Configure the data source -      <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">          < Propertyname= "Driverclassname"value= "${jdbc.driver}"/>        < Propertyname= "url"value= "${jdbc.url}"/>        < Propertyname= "username"value= "${jdbc.user}"/>        < Propertyname= "Password"value= "${jdbc.password}"/>    </Bean><!--Configure transaction manager -      <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">          < Propertyname= "DataSource"ref= "DataSource" />      </Bean>        <!--Configuring transaction Notification Properties -      <Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager">          <!--Defining transaction Propagation Properties -          <tx:attributes>              <Tx:methodname= "insert*"Propagation= "REQUIRED" />              <Tx:methodname= "update*"Propagation= "REQUIRED" />              <Tx:methodname= "edit*"Propagation= "REQUIRED" />              <Tx:methodname= "save*"Propagation= "REQUIRED" />              <Tx:methodname= "add*"Propagation= "REQUIRED" />              <Tx:methodname= "new*"Propagation= "REQUIRED" />              <Tx:methodname= "set*"Propagation= "REQUIRED" />              <Tx:methodname= "remove*"Propagation= "REQUIRED" />              <Tx:methodname= "delete*"Propagation= "REQUIRED" />              <Tx:methodname= "change*"Propagation= "REQUIRED" />              <Tx:methodname= "get*"Propagation= "REQUIRED"read-only= "true" />              <Tx:methodname= "find*"Propagation= "REQUIRED"read-only= "true" />              <Tx:methodname= "load*"Propagation= "REQUIRED"read-only= "true" />              <Tx:methodname="*"Propagation= "REQUIRED"read-only= "true" />          </tx:attributes>      </Tx:advice>               <!--Configure transaction Facets -      <Aop:config>          <Aop:pointcutID= "Serviceoperation"expression= "Execution (* com.lzl.sss.service). *.*(..))" />          <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Serviceoperation" />      </Aop:config>

About the transaction manager I'm using the JDBC transaction manager, and of course there are other things like

<BeanID= "TransactionManager"class= "Org.springframework.orm.hibernate3.HibernateTransactionManager">        < Propertyname= "Sessionfactory"ref= "Sessionfactory" /></Bean> <BeanID= "TransactionManager"class= "Org.springframework.orm.jpa.JpaTransactionManager">        < Propertyname= "Sessionfactory"ref= "Sessionfactory" />    </Bean>

Is the transaction manager for Hibernate and JPA (Java Persistence API transactions), respectively.

The above Pointcut expression allows transaction management of the methods under the Services package and its sub-packages.

 

--xml configuration implementation using AOP for transaction management in Spring MVC

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.