* 回滚事务 */ 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"> <!--配置业务层对象Service--> <bean id="accountService" class="com.ly.service.impl.AccountServiceImpl"> <!--注入dao对象--> <property name="accountDao" ref="accountDao"></property> </bean> <!--配置Data对象--> <bean id="accountDao" class="com.ly.dao.impl.AccountDaoImpl"> <!--注入QueryRunner--> <property name="runner" ref="runner"></property> <!--注入ConnectionUtils--> <property name="connectionUtils" ref="connectionUtils"></property> </bean> <!--配置QueryRunner--> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> </bean> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--连接数据库的必备信息--> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--配置Connection的工具类 ConnectionUtils--> <bean id="connectionUtils" class="com.ly.utils.ConnectionUtils"> <!--注入数据源--> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事务管理器--> <bean id="txManager" class="com.ly.utils.TransactionManager"> <property name="connectionUtils" ref="connectionUtils"></property> </bean> <!--配置aop--> <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);
}
**测试转账案例:** 首先我们先看我们数据库表中的数据: ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20201003210935126.png#pic_left) 执行测试代码:
@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错误,我们在看一下数据库表有没有发生改变。 ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20201003210917529.png#pic_center) 数据表并没有发生改变,实现了回滚。 ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20201003210959148.png#pic_left) **使用注解修改银行转账案例:** 修改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"> <!--配置spring创建容器时要扫描的包--> <context:component-scan base-package="com.ly"></context:component-scan> <!--配置QueryRunner--> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> </bean> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--连接数据库的必备信息--> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--开启spring对注解AOP的支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
在AccountServiceImpl中添加相关注解: ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20201003211640720.png#pic_left) 在AccountDaoImpl中添加相关注解: ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20201003211759645.png#pic_left) 在ConnectionUtils中添加相关注解: ![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=20201003211911420.png#pic_left) 在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) {
我将这三次阿里面试的题目全部分专题整理出来,并附带上详细的答案解析,生成了一份PDF文档
CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】
ava面试题,进阶技术大纲,架构资料分享
我将这三次阿里面试的题目全部分专题整理出来,并附带上详细的答案解析,生成了一份PDF文档
CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】
[外链图片转存中…(img-pzhtysE5-1630840945049)]
[外链图片转存中…(img-jjppzgLP-1630840945052)]
[外链图片转存中…(img-N6kxBA6A-1630840945053)]
[外链图片转存中…(img-Uid5s1nl-1630840945055)]
还有更多的Redis、MySQL、JVM、Kafka、微服务、Spring全家桶等学习笔记这里就不一一列举出来