gpt4 book ai didi

java - spring @Transactional 不为 rollbackFor=Exception.class 回滚

转载 作者:行者123 更新时间:2023-12-04 00:32:21 24 4
gpt4 key购买 nike

如果方法 solrJ.indexAllergenBulkSlor() 中发生任何异常,我需要回滚事务,但它不会回滚事务。我也将 AUTOCOMMIT 设置为 false。请帮忙。提前致谢。这就是我的服务实现的样子

@Service("mcareService")
@Transactional(readOnly = true, value = "oltpTransactionManager")
public class MyServiceImpl implements MyService {

@Override
@Transactional(value = "oltpTransactionManager", propagation=Propagation.REQUIRED, rollbackFor={Exception.class, SolrServerException.class})
public boolean saveAllergens(List<AllergenAutoCmp> allergenList) {
boolean flag = false;
LOGGER.info("Inside saveAllergens in MCareServiceImpl");
try {
allergenAutoCmpRepository.deleteAllergens();
allergenAutoCmpRepository.saveAllergens(allergenList);
solrJ.indexAllergenBulkSlor();
flag = true;
} catch (SolrServerException e) {
LOGGER.error("Error occured while solr indexing Allergens", e);
} catch (IOException e) {
LOGGER.error("Error occured while solr indexing Allergens", e);
}
LOGGER.info("returning from saveAllergens in MCareServiceImpl");
return flag;
}

}

最佳答案

这是因为你捕获了SolrServerException或者IOException,但是你并没有抛出。如果您了解 @Transational 的工作原理,您将意识到它围绕您的函数包装代码并试图捕获 RuntimeException。您需要将异常传递给外层。

 try {
allergenAutoCmpRepository.deleteAllergens();
allergenAutoCmpRepository.saveAllergens(allergenList);
solrJ.indexAllergenBulkSlor();
flag = true;
} catch (SolrServerException e) {
LOGGER.error("Error occurred while solr indexing Allergens", e);
throw new RuntimeException("SolrServerException occurred! Rollback my transaction.");
} catch (IOException e) {
LOGGER.error("Error occured while solr indexing Allergens", e);
throw new RuntimeException("IOException occurred! Rollback my transaction.");
}

关于java - spring @Transactional 不为 rollbackFor=Exception.class 回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364697/

24 4 0