I'm trying to upgrade our application with Hibernate 4.3.5.Final and Spring 4.0.6. Any where in my app with database write operation gets an error as below:
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$26.doInHibernate(HibernateTemplate.java:826)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.deleteAll(HibernateTemplate.java:823)
...
The follwing is my spring configuration for sessionFactory and transactionManager:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/mycompany/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
1:
In order to globally set the flushMode so that the app works the same way as before, I need to set flushMode to AUTO globally, so I don't want to use the @Transactional(readOnly = false) approach.
2:
In the post below, someone suggests setting singleSession to false, Java / Hibernate - Write operations are not allowed in read-only mode
The Spring documentations suggests that specifying "singleSession"="false" has side effect: http://docs.spring.io/spring/docs/4.0.6.RELEASE/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewInterceptor.html
3:
I have seen quite a few suggestions like the below in web.xml, which allows you to intercept the hibernate3 session and provide a version of the session with e.g. flushMode.AUTO. However, this doesn't work in hibernate 4 when you use org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
4:
The approach suggested below is using JPA transaction manager, which follows a complicate re-implementation of HibernateJpaDialect. I'm not using JPA at the moment and this approach doesn't seem to be simple enough. How do I set flush mode to "COMMIT" in my configuration files?
5:
I have tried having the following in my spring configuration (following a suggestion on Spring ORM 4.0.5 and Hibernate 4.3.5 - Cant save to database), it doesn't seem to work and people suggest using the web.xml approach: Spring and Hibernate suddenly set the transaction to readonly
<tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
Question:
Can anyone suggest a simple approach to allow setting FlushMode for Hibernate 4.3.5.Final and Spring 4.0.6?