在使用spring的注解事务的时候,需要考虑到事务的传播行为、遇到什么类型的异常时,事务才起作用、事务方法之间的嵌套调用时,怎么样才生效等等诸多问题。网上搜到很多的主要还是一堆理论文字描述,我这里给出亲测的代码,是借助公司真实的系统来做测试。

系统之间调用图如下:


spring注解事务使用总结-LMLPHP

事务和异步处理都在server模块里面。

接口如下:

  1. /**
  2. * 测试事务行为接口
  3. *
  4. * @author plg
  5. *
  6. */
  7. public interface TestService {
  8. public void methodA();
  9. public void methodB();
  10. public void methodC();
  11. .
  12. .
  13. .
  14. }
/**
* 测试事务行为接口
*
* @author plg
*
*/
public interface TestService { public void methodA(); public void methodB(); public void methodC(); .
.
.
}

使用两个表:

  1. CREATE TABLE `mall_order_statistics` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `shop_id` bigint(20) NOT NULL,
  4. `order_num` bigint(20) NOT NULL,
  5. `order_amount` bigint(20) NOT NULL,
  6. `avg_order_amount` bigint(20) NOT NULL,
  7. `pay_type` int(11) NOT NULL,
  8. `order_type` int(11) NOT NULL',
  9. `order_date` date NOT NULL,
  10. `create_time` datetime NOT NULL,
  11. PRIMARY KEY (`id`),
  12. KEY `idx_shop_id` (`shop_id`)
  13. ) ENGINE=InnoDB
  14. CREATE TABLE `mall_goods_ranking` (
  15. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  16. `shop_id` bigint(20) NOT NULL,
  17. `order_date` date NOT NULL,
  18. `mall_goods_id` bigint(20) NOT NULL,
  19. `goods_name` varchar(20) NOT NULL,
  20. `sales_volume` bigint(20) NOT NULL,
  21. `sales_amount` bigint(20) NOT NULL,
  22. `create_time` datetime NOT NULL,
  23. PRIMARY KEY (`id`),
  24. KEY `idx_shop_id` (`shop_id`)
  25. ) ENGINE=InnoDB
CREATE TABLE `mall_order_statistics` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`shop_id` bigint(20) NOT NULL,
`order_num` bigint(20) NOT NULL,
`order_amount` bigint(20) NOT NULL,
`avg_order_amount` bigint(20) NOT NULL,
`pay_type` int(11) NOT NULL,
`order_type` int(11) NOT NULL',
`order_date` date NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_shop_id` (`shop_id`)
) ENGINE=InnoDB CREATE TABLE `mall_goods_ranking` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`shop_id` bigint(20) NOT NULL,
`order_date` date NOT NULL,
`mall_goods_id` bigint(20) NOT NULL,
`goods_name` varchar(20) NOT NULL,
`sales_volume` bigint(20) NOT NULL,
`sales_amount` bigint(20) NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_shop_id` (`shop_id`)
) ENGINE=InnoDB

1.事务与异常类型

这里用的是注解式事务@Transactional。

1.1 正常的处理

先来一个正常的处理,事先已把数据库表清空,代码调用如下:

  1. web端代码如下:
  2. try {
  3. testService.methodA();
  4. } catch (Exception e) {
  5. logger.error("========================= " + e);
  6. }
  7. server端代码如下:
  8. @Transactional
  9. @Override
  10. public void methodA() {
  11. MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
  12. mallGoodsRanking.setShopId(1L);
  13. mallGoodsRanking.setOrderDate(new Date());
  14. mallGoodsRanking.setMallGoodsId(1L);
  15. mallGoodsRanking.setGoodsName("测试");
  16. mallGoodsRanking.setSalesVolume(1L);
  17. mallGoodsRanking.setSalesAmount(1L);
  18. mallGoodsRanking.setCreateTime(new Date());
  19. mallGoodsRankingMapper.insert(mallGoodsRanking);
  20. MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
  21. mallOrderStatistics.setShopId(1L);
  22. mallOrderStatistics.setOrderNum(1L);
  23. mallOrderStatistics.setAvgOrderAmount(1L);
  24. mallOrderStatistics.setOrderAmount(1L);
  25. mallOrderStatistics.setPayType();
  26. mallOrderStatistics.setOrderType();
  27. mallOrderStatistics.setOrderDate(new Date());
  28. mallOrderStatistics.setCreateTime(new Date());
  29. mallOrderStatisticsMapper.insert(mallOrderStatistics);
  30. }
web端代码如下:
try {
testService.methodA();
} catch (Exception e) {
logger.error("========================= " + e);
} server端代码如下:
@Transactional
@Override
public void methodA() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
mallOrderStatistics.setShopId(1L);
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
}

执行结果:

spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP
 

1.2异常处理-不插入必填字段,抛出RuntimeException

  1. @Transactional
  2. @Override
  3. public void methodA() {
  4. MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
  5. mallGoodsRanking.setShopId(1L);
  6. mallGoodsRanking.setOrderDate(new Date());
  7. mallGoodsRanking.setMallGoodsId(1L);
  8. mallGoodsRanking.setGoodsName("测试");
  9. mallGoodsRanking.setSalesVolume(1L);
  10. mallGoodsRanking.setSalesAmount(1L);
  11. mallGoodsRanking.setCreateTime(new Date());
  12. mallGoodsRankingMapper.insert(mallGoodsRanking);
  13. MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
  14. // mallOrderStatistics.setShopId(1L);必填字段
  15. mallOrderStatistics.setOrderNum(1L);
  16. mallOrderStatistics.setAvgOrderAmount(1L);
  17. mallOrderStatistics.setOrderAmount(1L);
  18. mallOrderStatistics.setPayType();
  19. mallOrderStatistics.setOrderType();
  20. mallOrderStatistics.setOrderDate(new Date());
  21. mallOrderStatistics.setCreateTime(new Date());
  22. mallOrderStatisticsMapper.insert(mallOrderStatistics);
  23. }
@Transactional
@Override
public void methodA() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
}

如上面的代码,把第二条插入语句的一个必填字段注释掉,这样执行之后,抛出了异常:

  1. ; SQL []; Column 'shop_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'shop_id' cannot be null, dubbo version: 2.8., current host: 192.168.9.141
  2. org.springframework.dao.DataIntegrityViolationException:
; SQL []; Column 'shop_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'shop_id' cannot be null, dubbo version: 2.8.4, current host: 192.168.9.141
org.springframework.dao.DataIntegrityViolationException:

抛出了DataIntegrityViolationException类型的异常,这个异常是RuntimeException类型的异常,spring的@Transactional默认就是捕捉RuntimeException异常,遇到RuntimeException异常才会处理事务回滚。

既然抛出了RuntimeException异常,那数据库自然没有插入成功了。接下来演示抛出Exception异常,看事务能否生效。

 

1.3异常处理-不插入必填字段,抛出Exception

  1. @Transactional
  2. @Override
  3. public void methodA() throws Exception {
  4. try {
  5. MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
  6. mallGoodsRanking.setShopId(1L);
  7. mallGoodsRanking.setOrderDate(new Date());
  8. mallGoodsRanking.setMallGoodsId(1L);
  9. mallGoodsRanking.setGoodsName("测试");
  10. mallGoodsRanking.setSalesVolume(1L);
  11. mallGoodsRanking.setSalesAmount(1L);
  12. mallGoodsRanking.setCreateTime(new Date());
  13. mallGoodsRankingMapper.insert(mallGoodsRanking);
  14. MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
  15. // mallOrderStatistics.setShopId(1L);必填字段
  16. mallOrderStatistics.setOrderNum(1L);
  17. mallOrderStatistics.setAvgOrderAmount(1L);
  18. mallOrderStatistics.setOrderAmount(1L);
  19. mallOrderStatistics.setPayType();
  20. mallOrderStatistics.setOrderType();
  21. mallOrderStatistics.setOrderDate(new Date());
  22. mallOrderStatistics.setCreateTime(new Date());
  23. mallOrderStatisticsMapper.insert(mallOrderStatistics);
  24. } catch (Exception e) {
  25. logger.error(" ================== " + e);
  26. throw new Exception("异常");
  27. }
  28. }
@Transactional
@Override
public void methodA() throws Exception {
try {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
} catch (Exception e) {
logger.error(" ================== " + e);
throw new Exception("异常");
}
}

捕捉异常后,然后 throw new Exception。执行后,抛出异常:

  1. java.lang.Exception: 异常
  2. at yunnex.saofu.mall.service.impl.TestServiceImpl.methodA(TestServiceImpl.java:)
  3. at yunnex.saofu.mall.service.impl.TestServiceImpl$$FastClassBySpringCGLIB$$8fcf06b.invoke(<generated>)
  4. at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:)
  5. at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:)
java.lang.Exception: 异常
at yunnex.saofu.mall.service.impl.TestServiceImpl.methodA(TestServiceImpl.java:55)
at yunnex.saofu.mall.service.impl.TestServiceImpl$$FastClassBySpringCGLIB$$8fcf06b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
执行结果:
spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP
 看到了吗,第一条SQL还是插入成功了,第二条语句插入出错,抛出Exception异常,但是事务并没有回滚。 接下来测试一下@Transactional(rollbackFor=Exception.class)这种情况。
 

1.4异常处理-不插入必填字段,抛出Exception,@Transactional指定异常类型

             由于篇幅问题,这里补贴代码了,和1.3不同的就是事务注解改成:@Transactional(rollbackFor = Exception.class),运行之后,事务生效:
spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP
 数据库表没有插入数据。
        对于service方法中使用注解事务,要么service中的方法中不做异常捕获,要么捕捉异常后throw new RuntimeException(),这样出现异常时,都会使事务回滚。如果需要捕捉特定类型的异常来回滚事务,则需要用@Transactional(rollbackFor=特定类型异常.class)来指定。
 =================================================================================================================================
 

2.事务方法嵌套使用

这里在引入一个表(mall_config),这个表原先是空的。

2.1事务方法调用私有方法,私有方法有数据库操作,私有方法抛出异常

  1. @Transactional
  2. @Override
  3. public void methodA() throws Exception {
  4. MallConfig mallConfig = new MallConfig();
  5. mallConfig.setCanFetch(true);
  6. mallConfig.setCanDeliver(true);
  7. mallConfigMapper.insert(mallConfig);
  8. this.insert();
  9. }
  10. private void insert() {
  11. MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
  12. mallGoodsRanking.setShopId(1L);
  13. mallGoodsRanking.setOrderDate(new Date());
  14. mallGoodsRanking.setMallGoodsId(1L);
  15. mallGoodsRanking.setGoodsName("测试");
  16. mallGoodsRanking.setSalesVolume(1L);
  17. mallGoodsRanking.setSalesAmount(1L);
  18. mallGoodsRanking.setCreateTime(new Date());
  19. mallGoodsRankingMapper.insert(mallGoodsRanking);
  20. MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
  21. // mallOrderStatistics.setShopId(1L);必填字段
  22. mallOrderStatistics.setOrderNum(1L);
  23. mallOrderStatistics.setAvgOrderAmount(1L);
  24. mallOrderStatistics.setOrderAmount(1L);
  25. mallOrderStatistics.setPayType();
  26. mallOrderStatistics.setOrderType();
  27. mallOrderStatistics.setOrderDate(new Date());
  28. mallOrderStatistics.setCreateTime(new Date());
  29. mallOrderStatisticsMapper.insert(mallOrderStatistics);//这里抛出异常
  30. }
@Transactional
@Override
public void methodA() throws Exception { MallConfig mallConfig = new MallConfig();
mallConfig.setCanFetch(true);
mallConfig.setCanDeliver(true);
mallConfigMapper.insert(mallConfig); this.insert();
} private void insert() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);//这里抛出异常
}

执行结果:

spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP

数据没有插入,事务生效!在同一个类中,一个事务方法调用私用方法,私用方法里有对数据库的操作,私有方法里有异常(RuntimeException)产生,这种情况事务是起效果的。

2.2 事务方法调用私有方法,私有方法有数据库操作,事务方法抛出异常

  1. @Transactional
  2. @Override
  3. public void methodA() throws Exception {
  4. MallConfig mallConfig = new MallConfig();
  5. mallConfig.setCanFetch(true);
  6. mallConfig.setCanDeliver(true);
  7. mallConfigMapper.insert(mallConfig);
  8. this.insert();
  9. //第三部分
  10. MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
  11. // mallOrderStatistics.setShopId(1L);必填字段
  12. mallOrderStatistics.setOrderNum(1L);
  13. mallOrderStatistics.setAvgOrderAmount(1L);
  14. mallOrderStatistics.setOrderAmount(1L);
  15. mallOrderStatistics.setPayType();
  16. mallOrderStatistics.setOrderType();
  17. mallOrderStatistics.setOrderDate(new Date());
  18. mallOrderStatistics.setCreateTime(new Date());
  19. mallOrderStatisticsMapper.insert(mallOrderStatistics);
  20. }
  21. private void insert() {
  22. MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
  23. mallGoodsRanking.setShopId(1L);
  24. mallGoodsRanking.setOrderDate(new Date());
  25. mallGoodsRanking.setMallGoodsId(1L);
  26. mallGoodsRanking.setGoodsName("测试");
  27. mallGoodsRanking.setSalesVolume(1L);
  28. mallGoodsRanking.setSalesAmount(1L);
  29. mallGoodsRanking.setCreateTime(new Date());
  30. mallGoodsRankingMapper.insert(mallGoodsRanking);
  31. }
@Transactional
@Override
public void methodA() throws Exception { MallConfig mallConfig = new MallConfig();
mallConfig.setCanFetch(true);
mallConfig.setCanDeliver(true);
mallConfigMapper.insert(mallConfig); this.insert(); //第三部分
MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
} private void insert() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking);
}

看以上代码,只有第三部分那里抛出异常,执行结果和2.1一样如下。在同一个类里面,一个事务方法里调用私有方法,相当于代码都写在事务方法里。

2.3 接口方法调用私有方法,私有方法添加事务注解

  1. @Override
  2. public void methodA() throws Exception {
  3. MallConfig mallConfig = new MallConfig();
  4. mallConfig.setCanFetch(true);
  5. mallConfig.setCanDeliver(true);
  6. mallConfigMapper.insert(mallConfig);
  7. this.insert();
  8. }
  9. @Transactional
  10. private void insert() {
  11. MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
  12. mallGoodsRanking.setShopId(1L);
  13. mallGoodsRanking.setOrderDate(new Date());
  14. mallGoodsRanking.setMallGoodsId(1L);
  15. mallGoodsRanking.setGoodsName("测试");
  16. mallGoodsRanking.setSalesVolume(1L);
  17. mallGoodsRanking.setSalesAmount(1L);
  18. mallGoodsRanking.setCreateTime(new Date());
  19. mallGoodsRankingMapper.insert(mallGoodsRanking);
  20. MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
  21. // mallOrderStatistics.setShopId(1L);必填字段
  22. mallOrderStatistics.setOrderNum(1L);
  23. mallOrderStatistics.setAvgOrderAmount(1L);
  24. mallOrderStatistics.setOrderAmount(1L);
  25. mallOrderStatistics.setPayType();
  26. mallOrderStatistics.setOrderType();
  27. mallOrderStatistics.setOrderDate(new Date());
  28. mallOrderStatistics.setCreateTime(new Date());
  29. mallOrderStatisticsMapper.insert(mallOrderStatistics);
  30. }
@Override
public void methodA() throws Exception { MallConfig mallConfig = new MallConfig();
mallConfig.setCanFetch(true);
mallConfig.setCanDeliver(true);
mallConfigMapper.insert(mallConfig); this.insert();
} @Transactional
private void insert() {
MallGoodsRanking mallGoodsRanking = new MallGoodsRanking();
mallGoodsRanking.setShopId(1L);
mallGoodsRanking.setOrderDate(new Date());
mallGoodsRanking.setMallGoodsId(1L);
mallGoodsRanking.setGoodsName("测试");
mallGoodsRanking.setSalesVolume(1L);
mallGoodsRanking.setSalesAmount(1L);
mallGoodsRanking.setCreateTime(new Date());
mallGoodsRankingMapper.insert(mallGoodsRanking); MallOrderStatistics mallOrderStatistics = new MallOrderStatistics();
// mallOrderStatistics.setShopId(1L);必填字段
mallOrderStatistics.setOrderNum(1L);
mallOrderStatistics.setAvgOrderAmount(1L);
mallOrderStatistics.setOrderAmount(1L);
mallOrderStatistics.setPayType(1);
mallOrderStatistics.setOrderType(1);
mallOrderStatistics.setOrderDate(new Date());
mallOrderStatistics.setCreateTime(new Date());
mallOrderStatisticsMapper.insert(mallOrderStatistics);
}

执行结果如下:

spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP
 
spring注解事务使用总结-LMLPHP
 出现异常后,前面两个表还是插入成功了。@Transactional放在private方法上是不起效果的,并且也不报错,spring官网也说明了这一点。实际上,像这种情况,内部调用带有事务注解的public方法,事务也不生效,也就是

  1. @Transactional
  2. public void insert() {
  3. 改成
  4. @Transactional
  5. private void insert() {
@Transactional
public void insert() { 改成 @Transactional
private void insert() {

具体原因请查看:http://blog.csdn.net/seelye/article/details/40144817。

 方法内部调用带有事务注解的方法(无论是private还是public),事务是不生效的。

spring异步(@Async)处理也是一样,有时候在进行业务处理的过程,有些业务可以作为异步来处理,这时候就有一些同学就在同一类中新建一个方法,使用@Async注解,然后调用这个异步方法,其实这是不起作用的,一定要新建另外一个类,这个类新建公共方法,用@Async注解,然后调用才起到异步的作用。

05-08 14:55