您需要将调用者方法放入一个事务中。
如果您有Spring事务环境,您可以将DAO服务/存储库的调用放在自己的服务/方法中,该方法标记为
@Transactional
,或者如果未启用事务支持,但应用程序中仍有spring支持,则可以使用
TransactionTemplate
直接,由弹簧提供
@Autowire
private PlatformTransactionManager txManager;
TransactionTemplate template = new TransactionTemplate(this.txManager);
template.execute( new TransactionCallback<Object>(){
public void doInTransaction(TransactionStatus status){
// work done here will be wrapped by a transaction and committed.
// status.setRollbackOnly(true) is called or an exception is thrown
}
});
否则,根据应用程序使用的技术,您必须自己手动处理事务性。