Java / Hibernate - Write operations are not allowed in read-only mode

北城余情 提交于 2019-11-26 22:34:50

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>

That error message is typically seen when using the Spring OpenSessionInViewFilter and trying to do persistence operations outside of a Spring-managed transaction. The filter sets the session to FlushMode.NEVER/MANUAL (depending on the versions of Spring and Hibernate you're using--they're roughly equivalent). When the Spring transaction mechanism begins a transaction, it changes the flush mode to "COMMIT". After the transaction completes, it sets it back to NEVER/MANUAL, as appropriate. If you're absolutely sure that this isn't happening, then the next most likely culprit is non-thread-safe use of a Session. The Hibernate Session must be used in only one thread. If it crosses over between threads, all kinds of chaos can happen. Note that an entity loaded from Hibernate can hold a reference to the Session in which it was loaded, and handing the entity across threads can thus cause the Session to be accessed from another thread, too.

add

@Transactional

above your function

I just stumbled onto this too. I needed to change the flush mode in Spring's OpenSessionInViewFilter to manual, and suddenly I started getting this exception. I found out that the problems were hapening in methods that were not annotated as @Transactional, so I guess that Spring implicitly treats all data access code outside such methods as read only. Annotating the method solved the problem.

Another way is to call the setCheckWriteOperations method on the HibernateTemplate object.

Use below bean for HibernateTemplate in application context.

<bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">  
        <property name="sessionFactory" ref="mysessionFactory"></property>
        <property name="checkWriteOperations" value="false"></property>
        </bean>
bigrowedogg

Try to use this

hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.setCheckWriteOperations(false);

The error should go away as the template is not checking if you are using a transaction.

You must forgot to add @Transactional annotation to your DAO service class/method, this will specify that your database operation will be handled by spring managed transaction.

user3198259

Below piece of code is worked for me.

hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.setCheckWriteOperations(false);

1) add @Transactional above you database operation methods and 2) make sure you have annotation driven transation manager

add

above HibernateTransactionManager configuration in applicationContext.xml file

I had the same problem and after one day of investigation I observed the following declaration

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

I removed

mode="aspectj"

and the problem disappeared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!