我认为以下stacktrace是java调用:
B.method2 (annotated with a plain @Transactional)
A.method1 (annotated with a plain @Transactional)
Main.main (starting point of the call, with no current transaction)
我希望在输入A.method1时启动事务 - 当剩下A.method1时,事务将被提交(或回滚)。我还希望在B.method2中使用相同的交易。
从B.method2中抛出RuntimeException。这是默认情况下为rollbackFor“列出”的异常。 Exception在A.method1中被捕获,但在离开B.method2时它将通过@Transactional的边界。
这是我的问题:(当前)事务是否会标记为回滚?
答案 0 :(得分:0)
默认传播模式为REQUIRED
,method2将使用针对method1启动的事务。在异常时,此事务将标记为回滚,因此不会将任何内容提交到数据库。您可以在method1之后获得UnexpectedRollbackException
。
这不是一个理想的行为,因为启动事务的代码(拥有它)应该控制回滚/提交。我会重新组织你的代码以避免这种可能性。