未针对未检查的异常回滚事务性方法


@Service
public class TransactionClass{
@AutoWire
TransactionClass tranClass;
@Autowire
TransactionRepository transRepo;
public void methodA(Data data){
try{
methodB(data)
}catch(Exception e){
//some logic
}
}
public void methodB(Data data){
//some logic
tranClass.methodC(data)
}
@Transactional
public void methodC(Data data){
//some logic
transRepo.save(data);
throw new RuntimeException();
}
}

问题是,即使抛出了未检查的异常,methodC((也不会被回滚。

要检查事务如何使用日志工作,只需将其添加到application.yaml

logging.level.org.springframework.transaction.interceptor: TRACE
logging.level.org.springframework.orm.jpa.JpaTransactionManager: DEBUG
logging.level.org.hibernate.SQL: DEBUG
spring.jpa.properties.hibernate.use_sql_comments: true

事务方法没有回滚,因为它有多个DB连接。因此@Transactional将回滚到唯一的主配置,并且我们在一个应用程序中也只能有一个主配置。对此的解决方案是使用链式事务。

请参阅此链接以了解有关链接交易的更多信息:https://blog.usejournal.com/springboot-jpa-rollback-transaction-with-multi-databases-53e6f2f143d6

最新更新