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 @   南风知君  阅读(196)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示