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对象
测试方法
分类:
Spring JDBC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!