Spring JDBC编程式事务与声明式事务

编程式事务

  编程式事务是指通过代码手动提交回滚事务的事务控制方法,SpringJDBC通过TransactionManager事务管理器实现事务控制,事务管理器提供commit/rollback方法进行事务提交与回滚

1.在applicationContext.xml中配置事务管理器

2.在需要事务控制的类里注入DataSourceTransactionManage

 

 

声明式事务

  声明式事务指在不修改源码情况下通过配置形式自动实现事务控制,声明式事务本质就是AOP环绕通知,当目标方法执行成功时,自动提交事务,当目标方法抛出运行时异常时,自动事务回滚

添加AspectJ依赖

schema文档

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.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">

配置TransactionManager事务管理器,

配置事务通知与事务属性,

为事务通知绑定PointCut切点

事务控制类不需要持有TransactionManage对象

 测试方法

 

posted @ 2022-04-29 16:42  南风知君  阅读(201)  评论(0)    收藏  举报