我们有 EntityManager persist() 和 @Transactional。如果插入的数据超过列长度,则 persist 不会抛出 DataIntegrityViolationException。当带有@Transactional的2级上层父方法返回时抛出。
class Worker {
@Transactional
public Integer createEmployee(String name) {
// employeeId is generated after checking if employee exists or not
saveEmployee(name, employeeId);
send_to_third_party(employeeId);
return employeeId;
}
saveEmployee(String name, Integer employeeId){
Employee em = new Employee();
// Get other data for employee like department etc.
dao.persist(employee);
}
}
class Dao {
@autowired
EntityManager em;
@Transational
public void persist(Object object) {
try{
em.persist(object);
log("successful");
}
catch(RuntimeException ex) {
log("persist failed");
throw ex;
}
}
}
如代码所示,即使出现数据长度大于列长度等错误,saveEmployee也会返回成功。将employeeId发送给第三方并进行处理。当createEmployee返回employeeId时,它会抛出异常“DataIntegrityViolationException”并显示消息“字符串或二进制数据将被截断”。最终,员工并没有保存在我们的数据库中,而是由第三方处理,这会在系统中造成不匹配和进一步的错误。
dao.persist()中的@Transactional不是提交持久化操作吗?为什么要花这么长时间才能抛出异常?
感谢您的帮助。
附注- 我们肯定会检查数据长度。
最佳答案
persist()
只会使对象被管理和持久化,在事务上下文关闭之前,它实际上不会对数据库执行任何操作。要强制立即与数据库同步,请在 persist()
之后调用 flush()
。这将导致立即抛出异常。
关于java - 带有 @Transactional 的 EntityManager persist() 不会抛出 DataIntegrityViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57788687/