spring 中的事务管理
-
在 Spring 框架中事务管理有两种方式:一种是传统的编程式事务管理,即通过编写代码实现的事务管理,就是用try catch代码块来捕捉错误;另一种是基于 AOP 技术实现的声明式事务管理,Spring 的声明式事务管理在底层采用了 AOP 技术,其最大的优点在于无须通过编程的方式管理事务,只需要在配置文件中进行相关的规则声明,就可以将事务规则应用到业务逻辑中。
-
Spring 实现声明式事务管理主要有两种方式:
- 基于 XML 文件方式的声明式事务管理。
- 通过 Annotation 注解方式的事务管理。
-
基于xml方式修改配置文件
<?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 http://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 https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--从classpath的根路径去加载db.properties文件--> <context:property-placeholder location="classpath:db.properties"/> <!--配置一个druid的连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置dao--> <bean id = "accountDao" class="com.test.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置service--> <bean id="accountService" class="com.test.service.impl.AccountServiceImpl"> <property name="dao" ref="accountDao"/> </bean> <!-- 1: 配置JDBC事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 2: 配置事务管理器增强 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="trans"/> </tx:attributes> </tx:advice> <!-- 3: 配置切面 --> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.test.service.*Service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
- CRUD通用事务配置
<!--配置一个CRUD的通用事务的配置--> <tx:advice id="crudAdvice" transaction-manager="txManager"> <tx:attributes> <!--service中的查询方法--> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> <tx:method name="list*" read-only="true" propagation="REQUIRED"/> <tx:method name="query*" read-only="true" propagation="REQUIRED"/> <!--service中其他方法(非查询)--> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
-
基于注解配置声明式事务
- 基于注解配置事务:在Service中,使用 @Transactional注解
- xml配置
<!--开启注解--> <context:annotation-config/> <!--配置IoC扫描包--> <context:component-scan base-package="com.test"/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置TX注解解析器--> <tx:annotation-driven transaction-manager="txManager"/> <context:property-placeholder location="classpath:db.properties"/> <!--配置一个druid的连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)