java – 抛出异常后回滚事务
作者:互联网
当我在服务方法中抛出异常时,我预计服务上的事务注释将回滚保存操作,但它不起作用.
这是我的服务:
@Service
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public class OrderServiceImp implements OrderService {
@Autowired
private OrderRepository orderRepository;
@Override
public void doSomeStaff(Long orderId) {
Order order = orderRepository.findOne(orderId);
orderRepository.save(order);
throw new NullPointerException("Test transaction exeption");
}
}
在data.xml中,我有下一个配置:
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="com.dmitro.repositories" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
在dispatcher-servlet.xml中我声明了scan:
<context:component-scan base-package="com.dmitro.service" />
我正在使用spring-data-jpa 1.8.0.RELEASE.请帮忙!
解决方法:
@Transactional(value = "transactionManagerForServiceLayer", rollbackFor = Exception.class)
这是罪魁祸首.您不应该为服务和存储库提供不同的事务管理器.要修复它,您需要将transactionManagerForServiceLayer替换为transactionManager,然后回滚将起作用.
标签:java,spring,spring-data-jpa,spring-transactions 来源: https://codeday.me/bug/20190708/1399763.html