Spring-12-声名式事务

Spring-12-声名式事务

要么都成功,要么都失败

事实上,除了声名式事务(基于AOP实现),还有编程式事务(在代码中try,catch,用事务管理器回滚),这里主要学习声名式事务

事务的ACID原则

  • 原子性
  • 一致性
  • 隔离性
  • 持久性

模拟事务

现在,模拟一个事务,添加一个用户的同时,删除一个用户

接口

public interface UserMapper {
    public List<User> getUsers();
    //添加一个用户
    public int addUser(User user);
    //删除一个用户
    public int deleteUser(int id);
    //测试事务
    public void txTest();
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cmy.dao.UserMapper">
    <select id="getUsers" resultType="User">
        select * from mybatis.user
    </select>
    
    <insert id="addUser" parameterType="User">
        insert into mybatis.user(id, name, pwd) 
        VALUES(#{id},#{name},#{pwd}) 
    </insert>
    
    <delete id="deleteUser" parameterType="int">
        deletes from mybatis.user where id=#{id}
    </delete>
</mapper>

注意,这里模拟了一个错误,sql语句deletes多了一个s

实现类

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
    public List<User> getUsers() {
        return sqlSession.getMapper(UserMapper.class).getUsers();
    }
    public int addUser(User user) {
        return sqlSession.getMapper(UserMapper.class).addUser(user);
    }
    public int deleteUser(int id) {
        return sqlSession.getMapper(UserMapper.class).deleteUser(id);
    }
   //这里模拟事务,添加4号用户,然后删除3号用户
    public void txTest(){
        User user = new User(4, "new", "445644");
        this.addUser(user);
        this.deleteUser(3);
    }
}

测试

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        userMapper.txTest();
    }
}

image-20210325142254416

可以看到,这违背了事务的ACID原则,在本例中,只增加了用户,确没有删除他,事务只完成了"一半"

可以类比预想,在转账过程中,用户的钱减少了而对方账户的钱没有增加!!!,这将造成严重的后果

声名式事务的实现

<!--配置声名式事务-->
<!--获得事务管理器,并注入数据源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织入,即在实现增删改查时,保证事务的特性-->
<!--配置事务通知:导入tx约束-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--给哪些方法配置事务-->
    <!--配置事务的传播特性7种 默认:propagation="REQUIRED"-->
    <tx:attributes>
        <!--可以指定方法,也可以直接全部方法-->
        <!--<tx:method name="add"/>-->
        <!--<tx:method name="delete"/>-->
        <!--<tx:method name="update"/>-->
        <!--<tx:method name="query" read-only="true"/>-->
        <!--所有方法-->
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<!--配置事务切入-->
<aop:config>
    <aop:pointcut id="txPointCut" expression="execution(* com.cmy.dao.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>

REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是Spring默认的选择。

SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。

MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。

REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。

NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。

NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

测试

image-20210325144400770

失败的事务都失败了,没有出现上例中的成功一半的情况

总结

  • 事务的存在十分必要,没有事务无法保证数据的一致性完整性

  • 如果不在Spring中配置声名式事务,也需要在代码中手动配置事务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值