/**
- 回滚事务
*/
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
- 释放连接
*/
public void release(){
try {
connectionUtils.getThreadConnection().close();
connectionUtils.removeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
配置bean.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:aop=“http://www.springframework.org/schema/aop”
xmlns:context=“http://www.springframework.org/schema/context”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
aop:config
<aop:pointcut id=“pt1” expression=“execution(* com.ly.service.impl..(…))”/>
<aop:aspect id=“txAdvice” ref=“txManager”>
<aop:before method=“beginTransaction” pointcut-ref=“pt1”></aop:before>
<aop:after-returning method=“commit” pointcut-ref=“pt1”></aop:after-returning>
<aop:after-throwing method=“rollback” pointcut-ref=“pt1”></aop:after-throwing>
<aop:after method=“release” pointcut-ref=“pt1”></aop:after>
</aop:aspect>
</aop:config>
AccountServiceImpl中添加相关的方法:
public void transfer(String sourceName, String targetName, Float money) {
System.out.println(“开始执行。。。”);
//2.执行操作
//2.1.根据名称查询转出账户
Account source=accountDao.findAccountByName(sourceName);
//2.2.根据名称查询转入账户
Account target=accountDao.findAccountByName(targetName);
//2.3.转出账户减钱
source.setMoney(source.getMoney()-money);
int a=1/0;
//2.4.转给账户加钱
target.setMoney(target.getMoney()+money);
//2.5.更新装出账户
accountDao.updateAccount(source);
//2.6.更新转入账户
accountDao.updateAccount(target);
}
测试转账案例:
首先我们先看我们数据库表中的数据:
执行测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = “classpath:bean.xml”)
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testTransfer(){
as.transfer(“aaa”,“bbb”,100f);
}
}
由于在转账过程中,转出账户后出现了/by zero错误,我们在看一下数据库表有没有发生改变。
数据表并没有发生改变,实现了回滚。
使用注解修改银行转账案例:
修改bean.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:aop=“http://www.springframework.org/schema/aop”
xmlns:context=“http://www.springframework.org/schema/context”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package=“com.ly”></context:component-scan>
aop:aspectj-autoproxy</aop:aspectj-autoproxy>
在AccountServiceImpl中添加相关注解:
在AccountDaoImpl中添加相关注解:
在ConnectionUtils中添加相关注解:
在TransactionManager中添加相关注解:
@Component(“txManager”)
@Aspect
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
@Pointcut(“execution(* com.ly.service.impl..(…))”)
private void pt1(){}
/**
- 开启事务
*/
//@Before(“pt1()”)
public void beginTransaction(){
try {
connectionUtils.getThreadConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
- 提交事务
*/
//@AfterReturning(“pt1()”)
public void commit(){
try {
connectionUtils.getThreadConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
- 回滚事务
*/
//@AfterThrowing(“pt1()”)
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
- 释放连接
*/
//@After(“pt1()”)
public void release(){
try {
connectionUtils.getThreadConnection().close();
connectionUtils.removeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Around(“pt1()”)
public Object aroundAdvice(ProceedingJoinPoint pjp) {
Object rtValue = null;
try {
//1.获取参数
Object[] args = pjp.getArgs();
//2.开启事务
this.beginTransaction();
//3.执行方法
rtValue = pjp.proceed(args);
//4.提交事务
this.commit();
//返回结果
return rtValue;
} catch (Throwable throwable) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。
还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)**
最后
我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。
[外链图片转存中…(img-vamLCSgm-1713397925754)]
还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。
[外链图片转存中…(img-U0pJ1Gi5-1713397925754)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!