The transaction

  • Treat a group of businesses as a transaction;
  • It’s all or nothing!
  • Transaction in the project development, very important, involving the consistency of data, can not be careless!
  • Ensure integrity and consistency;

The transactionACIDPrinciple:

  • atomic
  • consistency
  • Isolation,

Multiple services may operate the same resource to prevent data damage

  • persistence

Once committed, no matter what happens to the system, the results are no longer affected and persist to memory!

The sample

Let’s start with the tx namespace


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
Copy the code

Cut into the injection DataSourceTransactionManager, encapsulation, configuration


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <import resource="spring-config.xml"/>
    <bean id="peopleMapper" class="com.mybatis.DAO.PeopleMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>

    <bean id="transaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transaction">
        <tx:attributes> 
            <tx:method name="add"/>
            <tx:method name="getPeopleList"/>
            <tx:method name="delete"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.mybatis.DAO.*.*(..) )"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
    </aop:config>
</beans>
Copy the code

Mapper interfaces

public interface PeopleMapper {
    List<People> getPeopleList();
    int add(People people);
    int delete(int id);
}
Copy the code

Mapper implementation class

public class PeopleMapperImpl extends SqlSessionDaoSupport implements PeopleMapper{
 //private SqlSessionTemplate sqlSession;
// // all our operations are performed using sqLSession, in the original, now all chuan sqLSessionTemplate;
// public void setSqlSession(SqlSessionTemplate sqlSession) {
// this.sqlSession = sqlSession;
/ /}

    @Override
    public List<People> getPeopleList(a) {

        PeopleMapper mapper = getSqlSession().getMapper(PeopleMapper.class);
        mapper.add(new People(13."Jim".20."UK"));
        //mapper.delete(7);
    return mapper.getPeopleList();
    }

    @Override
    public int add(People people) {
        PeopleMapper mapper = getSqlSession().getMapper(PeopleMapper.class);
        return mapper.add(people);
    }

    @Override
    public int delete(int id) {
        PeopleMapper mapper = getSqlSession().getMapper(PeopleMapper.class);
        returnmapper.delete(id); }}Copy the code

Mapper.xml


      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mybatis.DAO.PeopleMapper">
    <select id="getPeopleList" resultType="com.mybatis.pojo.People">
        select * from mybatis.people;
    </select>
    <insert id="add" parameterType="com.mybatis.pojo.People">
        insert into mybatis.people (id, name,age,address) values (#{id},#{name},#{age},#{address});
    </insert>
    <delete id="delete" parameterType="int">
        delete from mybatis.people where id=#{id}
    </delete>
</mapper>
Copy the code

This avoids data inconsistencies, such as deleting successfully but adding failed

Bug collection



but was actually of type ‘com.sun.proxy.$Proxy‘* *