我将 spring 与 hibernate 结合使用,并使用 spring 事务管理器。我有以下方法,它是从另一个事务方法调用的。
@Transactional (readOnly = true, propagation = Propagation.REQUIRED)
public Map<String, String> getAllProperties ()
{ }
我面临的问题是;如果我运行这个,我会得到以下异常
Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failed
但是,如果我将注释更改为下一个注释或删除注释属性,它就可以正常工作
@Transactional (readOnly = false, propagation = Propagation.REQUIRED)
public Map<String, String> getAllProperties ()
{ }
我觉得有点奇怪,因为我只更改了 readOnly 属性。
有人可以解释一下吗
最佳答案
如果您想修改数据库中的某些数据,则无法使用readOnly = true
。
如果你想检索一些数据集也可以。
因此,当涉及到 UPDATE
或 INSERT
时,请使用 readOnly = false
。
祝你好运。
关于java - @Transactional(readOnly = false,propagation = Propagation.REQUIRED)抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243292/