Java / Hibernate - 在只读模式下不允许写入操作 [英] Java / Hibernate - Write operations are not allowed in read-only mode

查看:874
本文介绍了Java / Hibernate - 在只读模式下不允许写入操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

近来我一直有一个恼人的例外,经过对Google和这个论坛的一些研究后,我仍然没有找到可以解决我的问题的答案。



 有时候,我会遇到以下错误: org.springframework.dao.InvalidDataAccessApiUsageException:写操作是不允许在只读模式(FlushMode.NEVER /手动):将您的会话到FlushMode.COMMIT / AUTO或交易的定义中删除了readonly标志。在org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186)

。在org.springframework.orm.hibernate3.HibernateTemplate $ 12.doInHibernate(HibernateTemplate.java:696)
。在在org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
。在组织。 springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)

真奇怪的是有时当使用方法 getHibernateTemplate()。saveOrUpdate(object); 更新对象时,它可以工作,但有时使用相同的对象并通过调用相同的方法没有工作,但它似乎取决于我如何获得对象。



示例:假设我有一个包含3个字段的表:id,type , 长度。可能发生的是,如果我通过id获取对象并更新长度,那么它将起作用。如果我通过类型获取它并更新长度,那么它将不起作用。所以,为了避免这个问题,我一直在做的是获取这个方法以后不会导致问题的方法,但是这会变得越来越令人讨厌,试图找到一种可行的方法。



另外,现在我在尝试创建对象时(但不是所有对象,仅在一个特定的表上)出现此异常,并且找不到解决方法。我尝试在事务中添加 @Transactional(readOnly = false),但它没有改变任何内容,并且显示模式是说我没有处于read-只有反正。

有什么建议?

编辑七月二十六日:
这里有一些配置与hibernate

 < property name =hibernateProperties> 
<道具>
< prop key =jdbc.fetch_size> 20< / prop>
< prop key =jdbc.batch_size> 25< / prop>
< prop key =cglib.use_reflection_optimizer> true< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =connection.autoReconnect> true< / prop>
< prop key =connection.autoReconnectForPools> true< / prop>
< prop key =connection.is-connection-validation-required> true< / prop>
< /道具>
< / property>

另外,如果可以帮助的话

 < property name =transactionAttributes> 
<道具>
< prop key =get *> PROPAGATION_REQUIRED,readOnly< / prop>
< prop key =find *> PROPAGATION_REQUIRED,readOnly< / prop>
< prop key =execute *> PROPAGATION_REQUIRED< / prop>
< prop key =add *> PROPAGATION_REQUIRED< / prop>
< prop key =create *> PROPAGATION_REQUIRED< / prop>
< prop key =update *> PROPAGATION_REQUIRED< / prop>
< prop key =delete *> PROPAGATION_REQUIRED< / prop>
< /道具>
< / property>

编辑8月31日:
我的Class中的相关代码扩展了 HibernateDaoSupport ,保存对象是:

  public void createObject(Object persisObj){
getHibernateTemplate()。save(persisObj);
}


解决方案

从视图过滤器。解决问题:

 < filter> 
< filter-name> hibernateFilter< / filter-name>
< filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter< / filter-class>
< init-param>
< param-name> singleSession< / param-name>
< param-value> false< /参数值>
< / init-param>
< / filter>


I've been having an annoying exception a lot lately, and after some research on Google and this forum I still haven't found an answer that could solve my problem.

Here's the thing - sometimes, I get the following error when trying to update or create a new object with hibernate:

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.
at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)

What is really strange is that, sometimes when updating an object with the method getHibernateTemplate().saveOrUpdate(object); it will work, but sometimes with the same object and by calling the same method it doesn't work, but it seems to depend on how I get the object in the first place.

Example: let's say I have a table with 3 fields: id, type, length. What can happen is that, if I get the object by the id and update the length, then it will work. If I get it by the type and update the length, then it won't work. So what I've been doing so far to avoid the problem is to fetch the object the method that does not cause a problem later, but this is becoming more and more annoying to try and find a way that works.

Also, now I have this exception when trying to create an object (but not all of them, just on one specific table), and can't find a way for a workaround. And I tried to add @Transactional(readOnly = false) in the transaction but it didn't change anything, and displaying the mode was saying that I wasn't in read-only anyway.

Any suggestions?

Edit 26th of July: here's some configuration related to hibernate

<property name="hibernateProperties">
    <props>
        <prop key="jdbc.fetch_size">20</prop>
        <prop key="jdbc.batch_size">25</prop>
        <prop key="cglib.use_reflection_optimizer">true</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="connection.autoReconnect">true</prop>
        <prop key="connection.autoReconnectForPools">true</prop>
        <prop key="connection.is-connection-validation-required">true</prop>
    </props>
</property>

also, if it can help

<property name="transactionAttributes">
    <props>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="execute*">PROPAGATION_REQUIRED</prop>
        <prop key="add*">PROPAGATION_REQUIRED</prop>
        <prop key="create*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
    </props>
</property>

Edit 31st of August: The relevant code in my Class that extends HibernateDaoSupport, to save the objects is:

public void createObject(Object persisObj) {
    getHibernateTemplate().save(persisObj);
}

解决方案

I changed the single session propery from view filter. Problem solved:

  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
      <param-name>singleSession</param-name>
      <param-value>false</param-value>
    </init-param>
  </filter>

这篇关于Java / Hibernate - 在只读模式下不允许写入操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆