【问题标题】:Rollback not performed for @Transactional Annotation [duplicate]未为 @Transactional 注释执行回滚 [重复]
【发布时间】:2020-01-14 05:27:07
【问题描述】:

我正在尝试创建一个用于转移资金的 API 即取款和存款。 我使用@Transactional Annotation 执行了事务。 但是有一些标准,即如果银行帐号不存在,应该通过运行时异常。 我将附上代码。现在,当调用 transferBalanceMethod 时,如果存款人银行账户不存在,那么提取的金额也应该回滚。 但这并没有发生。表示当资金从 A 账户转移到 B 账户时,如果 B 的存款发生异常,则 A 账户中的提款也应该被提取。 我也尝试了@Transactional 注释以及 Exception 类的 rollbackFor 属性 我也尝试为存款和取款方法添加@Transaction 注释,但我们使用相同的交易,因为我们使用了需要传播**

Model Class//This is the Model Class
//All Imports

@Entity
public class BankAccount {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "bankAccountNumber", nullable = false,unique = true)
    @NotNull
    @Size(min = 5, message = "Bank account number should be greater than 5 characters")
    private String bankAccountNumber;

    @NotNull
    @Column(name = "balance", nullable = false)
    @Min(1000)
    private Long balance;

//Getter Setter and Constructor

**Controller File**//This is the Controller Class

//All imports and other stuff such as @RestController, @Autowired   

@GetMapping("/bankaccount/transfer")
public void transferBalance(@RequestParam("bankAccountNo1") String bankAccountNo1, @RequestParam("bankAccountNo2") String bankAccountNo2,
            @RequestParam("balance") Long balance) throws RuntimeException
    {
        bankService.transferBalance(bankAccountNo1,bankAccountNo2, balance);
    }

}

**Service File:-**//This is Service Layer
//All imports

@Service
public class BankService {

    @Autowired
    private BankRepository bankRepository;

    @Autowired
    private ModelMapper modelMapper;

    public List<BankAccountDTO> getAllBankAccount() {
        List<BankAccountDTO> bankAccountDTO = new ArrayList<BankAccountDTO>();
        List<BankAccount> bankAccount = bankRepository.findAll();
        for (BankAccount b : bankAccount) {
            bankAccountDTO.add(modelMapper.map(b, BankAccountDTO.class));
        }
        return bankAccountDTO;
    }

    public ResponseEntity<?> getIndividualBankAccount(String bankAccountNumber) {
        BankAccount bankAccount = bankRepository.findByBankAccountNumber(bankAccountNumber);
        if (bankAccount == null) {
            return new ResponseEntity<>("Account not found", HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(
                    modelMapper.map(bankRepository.findByBankAccountNumber(bankAccountNumber), BankAccountDTO.class),
                    HttpStatus.OK);
        }
    }

    public Object addBankAccount(BankAccountDTO bankAccountDTO) {
        return bankRepository.save(modelMapper.map(bankAccountDTO, BankAccount.class));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void depositBalance(String bankAccountNumber, Long balance) throws RuntimeException {
        BankAccount bankAccNo = bankRepository.findByBankAccountNumber(bankAccountNumber);

        if (bankAccNo == null) {
            throw new RuntimeException("Bank Accout Number is not found : " + bankAccountNumber);
        } else {
            if (balance <= 0) {
                throw new RuntimeException("Please deposit appropriate balance");
            } else {
                Long amount = bankAccNo.getBalance() + balance;
                bankAccNo.setBalance(amount);
                bankRepository.save(bankAccNo);
            }
        }

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void withdrawBalance(String bankAccountNumber, Long balance) throws RuntimeException {
        BankAccount bankAccNo = bankRepository.findByBankAccountNumber(bankAccountNumber);
        if (bankAccNo == null) {
            throw new RuntimeException("Bank Account not found :" + bankAccountNumber);
        } else {
            if (balance <= 0) {
                throw new RuntimeException("Please withdraw appropriate balance");
            } else {
                Long amount = bankAccNo.getBalance() - balance;
                if (amount < 1000) {
                    throw new RuntimeException("Sorry Cannot withdraw.Your minimum balance should be thousand rupees!");
                } else {
                    bankAccNo.setBalance(amount);
                    bankRepository.save(bankAccNo);
                }
            }
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = RuntimeException.class)
    public void transferBalance(String bankAccountNo1, String bankAccountNo2, Long balance) throws RuntimeException {
        try {
            withdrawBalance(bankAccountNo1, balance);
            depositBalance(bankAccountNo2, balance);

        } catch (RuntimeException e) {
            throw e;
        }

    }

}

【问题讨论】:

  • 你在使用 MySQL 和 MyISAM 表吗?
  • 是的,它们是 MyISAM 表
  • 那你不会得到交易,见 M.Deinum 的comment。
  • 如何将引擎从 MyISAM 更改为 InnoDB?
  • 它解决了我的问题。谢谢你,先生。

标签: spring spring-boot jpa transactions


【解决方案1】:

只有运行时异常才会触发 spring 事务注释中的回滚操作,如果您使用自定义注释,则需要确保您要么从 RuntimeException 扩展,要么向事务添加特定的回滚子句以便回滚在那个特定的例外中。

也许这个答案对你有用: Spring transaction: rollback on Exception or Throwable

还可以在此处查看 spring 的官方事务文档: https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多