使用Spring的声明式事务方法内部调用失效

当使用Spring的声明式事务时,必须注意Service服务类的方法,直接调用内部方法,是不会检测事务的,例如:

@Override

@Transactional(rollbackFor = {Exception.class})

public void updateEntity(UpdateActExCondConfPO po) throws Exception {

  updateEntityTwo(po);

  logger.info("更新procDefKey={},condKey={}的表达式配置", entity.getProcDefKey(), entity.getCondKey());

}


@Override

@Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRES_NEW)

public void updateEntityTwo(UpdateActExCondConfPO po) throws Exception {

  ActExConditionConf entity = actExConditionConfMapper.selectByPrimaryKey(po.getId());

  PropertyUtils.copyProperties(entity, po);

  entity.setLastUpdateTime(new Date());

  actExConditionConfMapper.updateByPrimaryKey(entity);

}

说明:

当调用updateEntity方法时,此方法调用的updateEntityTwo内部方法,是不会被检测updateEntityTwo方法上面的事务的。只能通过如果需要事务被正常检测使用,则需要通过SpringIOC容器中获取Serivce,然后通过Serice.updateEntityTwo()来调用。

如果想直接调用内部方法,可以使用TransactionTemplate来调用,调用方式如下:

transactionTemplate.execute(new TransactionCallback<Void>() {

  @Override

  public Void doInTransaction(TransactionStatus status) {

    updateEntityTwo(po);

    return null;

  }

});

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值