spring事务学习(转账案例)(二)
2023-09-27 14:20:23 时间
四、通过springAop进行事务管理
继续从第一个无事务操作的项目中进行更改。
只修改applicationContext.xml配置文件,注意设置transaction引用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_transaction"></property> <property name="user" value="root"></property> <property name="password" value="hjp123"></property> </bean> <!--配置Dao--> <bean id="accountDao" class="com.hujp.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置Service--> <bean id="accountService" class="com.hujp.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--事务管理器要从数据源中获得数据库连接进行操作--> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事务详情 <tx:advice>配置事务详情 id:通知名称 transaction-manager:事务详情最后要应用到平台(管理器) <tx:method>配置具体的详情 name详情名称,类似<prop key=""> 如果是transfer表示指定方法 add* 是以add开头的方法 * 任何方法 propagation传播行为 isolation隔离级别 read-only是否只读 rollback-for类似-Exception回滚 no-rollback-for类似+Exception提交 例如工作中类似配置:增删改查及所有 <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transfer" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--使用aop确定切入点 advice-ref通知确定事务详情 pointcut切入点表达式 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hujp.service..*.*(..))"></aop:advisor> </aop:config> </beans>
五、spring注解式管理事务
事务管理一般是在service层进行管理,所以继续从第一个无事务操作的项目中更改两个文件,一个是applicationContext.xml配置文件,另一个是AccountServiceImpl类
注解中参数方式配置事务详情
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_transaction"></property> <property name="user" value="root"></property> <property name="password" value="hjp123"></property> </bean> <!--配置Dao--> <bean id="accountDao" class="com.hujp.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置Service--> <bean id="accountService" class="com.hujp.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--将事务管理器交予spring,注解解析--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.hujp.service.impl; import com.hujp.dao.AccountDao; import com.hujp.service.AccountService; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * Created by JiaPeng on 2015/11/4. */ //@Transactional//可应用在类上 @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT) public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override //@Transactional//也可应用在方法上 public void transfer(String outUser, String inUser, int money) { this.accountDao.out(outUser, money); //模拟断电 //int m=2/0; this.accountDao.in(inUser, money); } }
相关文章
- Spring Cloud:微服务入门,案例准备
- Spring Data JPA 文档翻译的时候引用错误
- spring mvc 文件上传
- SpringBoot 源码解析 (二)----- Spring Boot精髓:启动流程源码分析
- Spring Cloud 多版本管理那些事。
- Spring Data Mongodb的API及案例(exists、gt、in、is、orOperator 、regex、size)
- Spring中的ApplicationListener的使用详解案例
- spring-boot-starter-thymeleaf 避坑指南
- Spring Data JPA 中常用注解
- spring boot 生成 war 包有一个war.original是什么?
- Spring的线程池ThreadPoolTaskExecutor使用案例
- 每日一博 - 常见的Spring事务失效&事务不回滚案例集锦(事务失效)
- spring成神之路第三十九篇:强大的 Spel 表达式
- [Spring Framework]AOP经典案例、AOP总结
- spring的IOC,DI及案例详解
- 【微服务 从0开始 】Spring Cloud 配置文件
- Spring Boot2 系列教程(三)理解 Spring Boot 项目中的 parent
- 【Spring AOP】@Aspect结合案例详解(二): @Pointcut使用@within和within(已附源码)
- Spring Batch 综合案例实战中
- Spring Batch 批处理入门案例
- Spring Cloud Ribbon——客户端负载均衡