10.spring declarative transaction

I. Review of affairs

  • Transaction very important in the project development process, involving the issue of consistency of data, can not be sloppy!

  • Transaction management is enterprise-class application development techniques necessary, to ensure the integrity and consistency of the data.

1. The transaction is to a series of operations as an independent unit of work, these actions are either completed or all does not work.

2. Transaction four properties ACID

  1. Atomicity (Atomicity)

    • Transaction is an atomic operation, a series of actions composed of, atomic transactions to ensure that the operation of either completed or totally ineffective

  2. Consistency (consistency)

    • Once all actions to complete the transaction, the transaction must be submitted. Data and resources in a consistent state that satisfies the business rules in

  3. Isolation (Isolation)

    • A plurality of transactions may be processed simultaneously the same data, so that each transaction with other transactions should be isolated to prevent data corruption

  4. Persistence (durability)

    • Once the transaction is completed, no matter what system error occurs, the result will not be affected. Typically, the outcome of the transaction is written to persistent storage

Organizer of two .Spring

1. Test the consistency of the transaction

:( step on the basis of a project on the blog)

  1. Add two interface methods UserMapper

    1. addUser: Add User

    2. deleteUser: Delete User

  2. In UserMapper.xml writing SQL statements, the written deliberately delete deletes lead to errors

  3. Modify UserMapperImpl class

    1. Add addUser method

    2. Add deleteUser method

    3. Modify selectUser method, first add it to a user, and returns all users to delete a user

Add two interface methods UserMapper

. 1  public  interface UserMapper {
 2  
. 3      List <the User> selectUser ();
 . 4  
. 5      // add the user 
. 6      int the addUser (the User User);
 . 7  
. 8      // delete users 
. 9      int deleteUser ( int ID);
 10 }

In UserMapper.xml writing SQL statements, the written deliberately delete deletes lead to errors

1 <insert id="addUser" parameterType="user">
2     insert into mybatis.user (id, name, pwd) value (#{id},#{name},#{pwd});
3 </insert>
4 
5 <delete id="deleteUser" parameterType="_int">
6     deletes from mybatis.user where id = #{id};
7 </delete>

Modify UserMapperImpl class

 1 public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
 2 
 3 
 4     public List<User> selectUser() {
 5         User user = new User(7,"小明","123456");
 6         UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
 7         mapper.addUser(user);
 8         mapper.deleteUser(7);
 9         return mapper.selectUser();
10     }
11 
12     @Override
13     public int addUser(User user) {
14         return getSqlSession().getMapper(UserMapper.class).addUser(user);
15     }
16 
17     @Override
18     public int deleteUser(int id) {
19         return getSqlSession().getMapper(UserMapper.class).deleteUser(id);
20     }
21 }

Directly run the test program found that the addition of the user but does not delete the user, the transaction is not rolled back the consistency is destroyed

2.Spring transaction management into two categories:

  • Programmatic transaction management (well, do not use)

    • The transaction management code is embedded into business processes to control transaction commit and rollback

    • Cons: Must contain additional transaction management code in each transaction business logic operation

  • Declarative transaction management (good use)

    • Easy to use than programmatic transaction under normal circumstances.

    • The transaction management code is separated from the business methods, declaratively to implement transaction management.

    • The transaction management as a crosscutting concerns, modularity by aop method. Spring declarative transaction management supported by Spring AOP framework.

3. Use declarative transaction management to ensure that the transaction ACID

step:

  1. Modified spring-dao.xml, introduced into the head restraint information

  2. Modify spring-dao.xml

    1. Add declarative transaction

    2. Add AOP configuration combines transactional advice

    3. Add a combination of AOP configuration tangent point

Modified spring-dao.xml, introduced into the head restraint information tx

  •  xmlns:tx="http://www.springframework.org/schema/tx" 
  •  http://www.springframework.org/schema/tx 
  •  http://www.springframework.org/schema/tx/spring-tx.xsd 
 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:tx="http://www.springframework.org/schema/tx"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop
 8         http://www.springframework.org/schema/aop/spring-aop.xsd
 9         http://www.springframework.org/schema/tx
10         http://www.springframework.org/schema/tx/spring-tx.xsd">

Modify spring-dao.xml

1 <--4! Configuration statement transaction.
 2          REF: Our data source reference
 . 3      ->
 . 4      <the bean ID = "the transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
 . 5          <Property name = "the dataSource" REF = "the dataSource" />
 . 6      </ the bean>
 . 7  
. 8      !. <-. 5 binding AOP cut, to ensure the consistency of the transaction ->
 9      <! - configuration transaction notification ->
 10      <TX: ID = the advice "txAdvice" transaction Manager-= "the transactionManager">
 . 11          <TX: Attributes>
 12 is              ! <- configuration which means what kind of transaction using,
Configuration transaction propagation characteristic 13 is                  . 1The propagation characteristics of the transaction: a plurality of transaction calls another method, how transactions propagate between these methods
 14                  2.read =-only "to true" : Read-only data is not operational
 15                  . 3 .propagation: propagation characteristics for configuring the transaction
 16                  = 4.propagation "REQUIRED" : If no transaction creates a new transaction, if there is a transaction, added to this transaction, which is the most common choice (default).
. 17              ->
 18 is              <TX: Method name = "the Add" propagation = "REQUIRED" />
 . 19              <TX: Method name = "Delete" propagation = "REQUIRED" />
 20 is              <TX: Method name = "Update" propagation = "REQUIRED" />
 21 is              <TX: Method name = "Query" = Read-only "to true" />
         </ TX: Attributes>
 24      : </ TX the advice>
 25  
26 is      <! -. 6 . AOP configuration transaction cut
 27          disposed tangent point for all classes and methods in packet mapper for ensuring transaction the ACID
 28      ->
 29      <AOP: config>
 30          <AOP: the pointcut ID = "txPointcut" = expression The "Execution (ustc.wzh.mapper * * * (..)..)" />
 31 is          <AOP: the advice-REF = Advisor "txAdvice" = REF-the pointcut "txPointcut" />
 32      </ AOP: config>

At this time, the test found no added transaction rollback occurs to ensure consistency

The delete statement in the repair UserMapper.xml, still execute successfully, ensure transactional consistency

4. Summary:

Why do I need to configure affairs?

  • If you leave, you need to submit our control transactions manually;

  • Transaction very important in the project development process, involving the issue of consistency of data, can not be sloppy!

 

Guess you like

Origin www.cnblogs.com/zhihaospace/p/12329488.html