java HIbernate InvalidDataAccessApiUsageException - 只读模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3874835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HIbernate InvalidDataAccessApiUsageException - read-only mode
提问by Nathan Spears
Summary: the Exception is telling me that the transaction is read-only; a debug println seems to indicate that I'm not in read-only mode.
总结:异常告诉我事务是只读的;调试 println 似乎表明我不是处于只读模式。
Classes edited for internet publishing - sorry if I mistyped something but this is the jist of the code giving me problems. saveOrUpdate works when called on other object types but not on this one. I added the println to saveOrUpdate as I was debugging. I didn't write the abstract class, I'm just trying to use it (and now debug it).
为 Internet 发布而编辑的类 - 对不起,如果我输入错误,但这是给我带来问题的代码的 jist。saveOrUpdate 在其他对象类型上调用时起作用,但在这个对象类型上不起作用。我在调试时将 println 添加到 saveOrUpdate 。我没有写抽象类,我只是想使用它(现在调试它)。
Relevant output below code. Not sure where to go from here.
代码下方的相关输出。不知道从这里去哪里。
Update after investigation: I have also been in the middle of doing some updates to the spring config and a coworker pointed out that one method I called updateAParameter from was using spring in one way, and the broken method was using it in another way. Unfortunately the broken way is the way I was trying to get to.
调查后更新:我也一直在对 spring 配置进行一些更新,一位同事指出我调用 updateAParameter 的一种方法正在以一种方式使用 spring,而损坏的方法正在以另一种方式使用它。不幸的是,这条坏路是我试图达到的方式。
So the problem as I now understand it is that if I instantiate the DataObjectDAOImpl "manually" in a method by getting a bean, then it allows me to write back to Hibernate correctly. If I use spring to set a class variable for that bean so I don't have to instantiate it in every method, then the InvalidDataAccessApiUsageException occurs when I access a method that tries to write to Hibernate, despite the fact that it reports not being in read-only mode. My coworker had a theory on this topic but I didn't understand what he was trying to say - something about extracting an interface from the SampleClass.
所以我现在理解的问题是,如果我通过获取 bean 在方法中“手动”实例化 DataObjectDAOImpl,那么它允许我正确地写回 Hibernate。如果我使用 spring 为那个 bean 设置一个类变量,所以我不必在每个方法中实例化它,那么当我访问一个尝试写入 Hibernate 的方法时会发生 InvalidDataAccessApiUsageException,尽管它报告不在只读模式。我的同事有一个关于这个主题的理论,但我不明白他想说什么 - 一些关于从 SampleClass 中提取接口的东西。
// Old way that works.
public class SampleClass {
public void someMethod {
ApplicationContext ac = ApplicationContextFactory.getApplicationContext();
DataObjectDAOImpl dodi = ((DataObjectDAOImpl) ac.getBean("dodi"));
//this works
dodi.updateAParameter("foo", exampleDataObject);
}
}
//New way that doesn't work but I would like it to.
public class SampleClass {
private DataObjectDAOImpl dodi = null;
//'dodi' has getter and setter methods that I am not pasting here for simplicity
public void someMethod {
//causes Exception
dodi.updateAParameter("foo", exampleDataObject);
}
}
and here is relevant bean from the spring config
这是来自 spring 配置的相关 bean
<bean id="sampleclass" class="com.service.SampleClass" scope="prototype">
<property name="dodi" ref="doDAOimpl"/>
</bean>
here is the DAOImpl which is the same for the old and new way
这是新旧方式相同的 DAOImpl
public class DataObjectDAOImpl extends AbstractSpringDaoStuff {
...
public void updateAParameter(String parameter, DataObject do) {
do.setAParameter(parameter);
super.saveOrUpdate(do);
}
}
public abstract class AbstractSpringDaoStuff extends HibernateDaoSupport {
...
@Transactional(readOnly=false)
protected void saveOrUpdate(Object obj) {
System.out.println ( "Read-only?: " + TransactionSynchronizationManager.isCurrentTransactionReadOnly () );
getHibernateTemplate().saveOrUpdate(obj);
}
}
Output from the app server:
应用服务器的输出:
[java] Read-only?: false
[java] - Method execution failed:
[java] org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
[java] at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186)
[java] at org.springframework.orm.hibernate3.HibernateTemplate.doInHibernate(HibernateTemplate.java:750)
[java] at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
[java] at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
[java] at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748)
[java] at com.est.dao.AbstractSpringDaoStuff.saveOrUpdate(AbstractSpringDaoMDA.java:24)
etc
采纳答案by axtavt
The one possible problem I can see here is that you are calling @Transactionalmethod from the same bean. I'm not sure how it can be related to your exception, but since Spring's declarative transaction management is implemented via proxy-based AOP it means that this annotation doesn't take effect.
我在这里看到的一个可能的问题是您正在@Transactional从同一个 bean调用方法。我不确定它如何与您的异常相关,但由于 Spring 的声明性事务管理是通过基于代理的 AOP 实现的,这意味着此注释不会生效。
See also:
也可以看看:

