如果在持久化实体 X 时抛出异常,是否可以在 catch 块中调用不同的方法来持久化某个实体 Y?
@Transactional(propagation = Propagation.REQUIRED)
private X addNewX(X transientX) {
X x = null;
try {
x = xDao.makePersistent(transientX); // A DB constraint will be violated and Hibernate throws PersistenceException
} catch (RuntimeException e) {
createErrorRecord(transientX, e.getMessage());
}
return x;
}
保存有关错误的额外信息:
@Transactional(propagation = Propagation.REQUIRED)
private void createErrorRecord(X x, String errMsg) {
try {
ImportError error = new ImportError(x.getBlah(), x.moreBlah(),
errMsg);
impErrDao.makePersistent(error);
} catch (RuntimeException re) {
logger.error(re.toString());
}
}
但是,ImportError
永远不会坚持下去。我尝试过noRollbackFor
,PersistenceException
但无济于事。
有什么可以在这里做的吗?谢谢