写点什么

Spring-- 基于 AOP 实现事务控制

作者:Java高工P7
  • 2021 年 11 月 11 日
  • 本文字数:3087 字

    阅读完需:约 10 分钟

connectionUtils.getThreadConnection().setAutoCommit(false);


} catch (SQLException e) {


e.printStackTrace();


}


}


/**


  • 提交事务


*/


public void commit(){


try {


connectionUtils.getThreadConnection().commit();


} catch (SQLException e) {


e.printStackTrace();


}


}


/**


  • 回滚事务


*/


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/


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


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">


<bean id="accountService" class="com.ly.service.impl.AccountServiceImpl">


<property name="accountDao" ref="accountDao"></property>


</bean>


<bean id="accountDao" class="com.ly.dao.impl.AccountDaoImpl">


<property name="runner" ref="runner"></property>


<property name="connectionUtils" ref="connectionUtils"></property>


</bean>


<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>


<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: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>


</beans>


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>


<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>


aop:aspectj-autoproxy</aop:aspectj-autoproxy>


</beans>


在 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.执行方法

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
Spring--基于AOP实现事务控制