Announcement Announcement Module
Collapse
No announcement yet.
Error with FlushMode in Spring 3.1 Rest and Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error with FlushMode in Spring 3.1 Rest and Hibernate

    Hello,

    I am trying to make it work for Spring 3.1 Rest Webservice and Hibernate in a test project. All is good if i need to retrieve informations using the GET method, but if I call methods that use a POST method things stop working and it's
    throwing me this error:
    HTML Code:
    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.
    Below is my persistence.xml configuration:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    	
    	<!-- Load in application properties reference -->
    	<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	    <property name="location" value="classpath:be/taximobile/rest/resources/database.properties"/>
    	</bean>
    	
    	<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${db.driverClassName}" />
    		<property name="url" value="${db.url}" />
    		<property name="username" value="${db.username}" />
    		<property name="password" value="${db.password}" />
    	</bean>
    	
    <!-- Hibernate SessionFactory -->
       <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
          <property name="dataSource">
             <ref local="mysqlDataSource" />
          </property>
    
          <property name="annotatedClasses">
             <list>
                <value>test.rest.bean.Client</value>
                
             </list>
          </property>
          <property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLDialect
    				</prop>
    				<prop key="hibernate.hbm2ddl.auto">create</prop>
    				<prop key="lazy">true</prop>
    				<prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.generate_statistics">true</prop>
                    <prop key="hibernate.use_sql_comments">true</prop>
    			</props>
    		</property>
       </bean>
       
    	<!-- Configure Transaction manager for a single Hibernate SessionFactory(because we are using one database source only, and so a single Session Factory) -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    		<property name="rollbackOnCommitFailure" value="true" />
    	</bean>
    	
       <bean id="transactionProxyTemplate" abstract="true"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
          <property name="transactionManager">
             <ref local="transactionManager" />
          </property>
          <property name="transactionAttributes">
             <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
             </props>
          </property>
       </bean> 
    
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	
    	<!-- define DAO and Service beans -->
    	<bean id="clientDao" class="be.taximobile.rest.persistence.ClientDAOImpl">
    	    <property name="hibernateTemplate" ref="hibernateTemplate" />
            </bean>
        
    	<bean id="clientService"
    		class="be.taximobile.rest.services.ClientServiceImpl" >
            <property name="clientDao">
    			<ref bean="clientDao" />
    		</property>
    	</bean>
    	
    	<!-- end -->
    	
    	<bean name="openSessionInViewInterceptor"
    		class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    		<property name="flushModeName" value="FLUSH_AUTO" />
    	</bean>
    </beans>
    My web.xml is:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
        metadata-complete="true">
        
    	<display-name>test.rest</display-name>
    	
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    	
    	<!-- The context params that read by ContextLoaderListener  -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/Backend-context.xml
    		</param-value>
    	</context-param>
    	
    	<!-- This listener will load other application context file in addition to springweb-servlet.xml -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	
    	<servlet>
    		<servlet-name>Backend</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>Backend</servlet-name>
    		<url-pattern>/service/*</url-pattern>
    	</servlet-mapping>
    	
    	<jsp-config>
    		<taglib>
    			<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
    			<taglib-location>/WEB-INF/tags/c.tld</taglib-location>
    		</taglib>
    	</jsp-config>
    	 <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/log4j.properties</param-value>
        </context-param>
        
    	 <listener>
    	    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	 </listener>
    	 
    	 <filter>
          <filter-name>hibernateFilter</filter-name>
          <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
          <init-param>
             <param-name>sessionFactoryBeanName</param-name>
             <param-value>sessionFactory</param-value>         
          </init-param>      
       	</filter>
       
    	 <filter-mapping>
    	   <filter-name>hibernateFilter</filter-name>
    	    <url-pattern>/*</url-pattern>
    	  </filter-mapping>
    	  
    </web-app>
    I don't understand why i get this error because i set the Flush mode to Auto, and i don't use @Transactional annotation anywhere in controllers or services.

    Did you encounter this problem? I think i need a new pair of eyes to look at this because i cannot figure it out.
    Thank you.

  • #2
    Cause could be OpenSessionInViewFilter, as per javadoc, the flush mode in this filter by default is set to FlushMode.NEVER. In case you are performing write operations you should either use @Transactional or set flush mode to FlushMode.AUTO. My recommendation will be to use @Transactional as this will protect against accidental updates. Please refer to

    http://static.springsource.org/sprin...iewFilter.html

    Comment


    • #3
      Hello, thank you for your response. I tried two things: comment the OpenSessionInViewFilter and just use Session Factory in the persistence layer. The latter made it possible to insert data in the DB, but it inserts only null's. From my request it does not take the object i am sending .
      This is my code:
      Code:
      SessionFactory sessionFactory = getHibernateTemplate()
      				.getSessionFactory();
      		Session session = sessionFactory.openSession();
      		session.save(client);
      		session.flush();
      And here "client" is forever NULL.
      I don't understand at all. I have tried with keeping the OpenSessionInViewFilter commented in the xml and use annotations @Transactional on the persisten DAO interface:
      Code:
      @Transactional
      public interface ClientDAO {
      public Client saveClient(Client client);}
      but still nothing...
      Last edited by gabriela; Aug 31st, 2012, 07:23 AM.

      Comment


      • #4
        You should try using the @Transactional annotation on the Impl, sometimes applying annotation on interface does not work. Also was the OpenSessionInViewFilter filter enabled when you tried the @Transactional annotation?

        You can also try setting the flush mode for OpenSessionInViewFilter.

        Comment


        • #5
          I have in persistence.xml file commented:
          Code:
          	<!-- end
          	
          	<bean name="openSessionInViewInterceptor"
          		class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
          		<property name="sessionFactory">
          			<ref bean="sessionFactory" />
          		</property>
          		<property name="flushModeName" value="FLUSH_AUTO" />
          	</bean> -->
          Then in ClientDao implementation class i have put the @Transactional annotation.
          Last edited by gabriela; Aug 31st, 2012, 07:23 AM.

          Comment


          • #6
            I forgot to mention, i have tried with Spring 3.1 and Spring 3.0. Same output. Very much lost.

            Comment

            Working...
            X