要更新实体Attribute
,下面的一段代码会调用事务模板(而不是@Transactional
,因为只需要部分方法是事务性的)
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
updateAttributeIsCustom(entry.getKey(), Arrays.asList(createUser, createDate, modifiedUser, modifiedDate, "CustomerKey", "ProfileId", "JobId", "PageSequence"), true);
}
});
updateAttributeIsCustom
方法如下:
private void updateAttributeIsCustom(DalDataList dalDataList, List<String> auditableColumnNames, boolean isReset) {
List<DalAttribute> attributes = dalDataList.getAttributes();
DalUser currentUser = attributeDao.getReference(DalUser.class,
coreService.getUserContext().getUserId());
Boolean isCustom = null;
try {
for (DalAttribute attribute : attributes) {
if(!isReset) {
String customValue = String.valueOf(!auditableColumnNames.contains(attribute.getName()));
if (dalDataList.getListType() == DataListType.TRANSACTION_TABLE && (attribute.getName().equals("CREATED_BY") || attribute.getName().equals("CREATED_DATE"))) {
customValue = "false";
}
isCustom = Boolean.valueOf(customValue);
}
attribute.setCustom(isCustom);
attribute.setModifiedBy(currentUser);
attributeDao.update(attribute);
}
} catch (Exception e) {
logger.error("Error occured during during updation of list with id {}. Attribute Sync is required.", dalDataList.getId());
}
}
现在,当执行这段代码时,它在我的本地环境中运行良好。但是,这在较高的环境中失败,同一数据集表明存在以下错误:
java.lang.Exception: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly\n\tat
事务未在任何地方显式设置为rollBackOnly。只有当isReset为true时才会发生错误,而当它为false时它会正常工作。什么可能导致这个错误?