facebook

Write operations are not allowed in read-only mode

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #268567 Reply

    denov
    Member

    i’m trying to setup a new project using spring and hibernate. i have an old project that was done in (plain old) eclipse. the big difference on my new myEclipse project is i used the reverse engineering hibernate tool to generate all my Pojos and DAO. When i try to do a save i get this exception. i’ve read a bunch of post on the net about related problems. i still really don’t have a clean understand what why this is and how to resolve it

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) – turn your Session into FlushMode.AUTO or remove ‘readOnly’ marker from transaction definition
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:406)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) – turn your Session into FlushMode.AUTO or remove ‘readOnly’ marker from transaction definition
    org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1098)
    org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:616)
    org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
    org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:614)
    com.autoreturn.dao.AuctionDAO.save(AuctionDAO.java:37)
    com.autoreturn.manager.AuctionManagerImpl.createAuction(AuctionManagerImpl.java:27)
    com.autoreturn.controller.validator.AuctionCreateValidator.validate(AuctionCreateValidator.java:28)
    org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:55)
    org.springframework.web.servlet.mvc.BaseCommandController.bindAndValidate(BaseCommandController.java:372)
    org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:248)
    org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    applicatonContext.xml

    <bean id=”sessionFactory”
    class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
    <property name=”configLocation”>
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean>

    <bean id=”transactionManager”
    class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
    <property name=”sessionFactory”>
    <ref bean=”sessionFactory” />
    </property>
    </bean>

    <bean id=”transactionProxyTemplate” abstract=”true”
    class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean”>
    <property name=”target”><ref bean=”auctionManager”/></property>
    <property name=”transactionManager”>
    <ref local=”transactionManager” />
    </property>
    <property name=”transactionAttributes”>
    <props>
    <prop key=”*”>PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <!– ************** Managers ************** –>

    <bean id=”auctionManager”
    class=”com.autoreturn.manager.AuctionManagerImpl”>
    <constructor-arg index=”0″ ref=”AuctionDAO” />
    </bean>

    <!– *********** DAOs ************ –>

    <bean id=”AuctionDAO” class=”com.autoreturn.dao.AuctionDAO”>
    <property name=”sessionFactory”>
    <ref bean=”sessionFactory” />
    </property>
    </bean>

    spirng-servlet.xml

    <bean id=”beanMapping”
    class=”org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”>
    <property name=”interceptors”>
    <list>
    <ref bean=”openSessionInViewInterceptor” />
    </list>
    </property>
    </bean>

    <bean name=”openSessionInViewInterceptor”
    class=”org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor”>
    <property name=”sessionFactory”>
    <ref bean=”sessionFactory” />
    </property>
    </bean>

    <bean id=”messageSource”
    class=”org.springframework.context.support.ResourceBundleMessageSource”>
    <property name=”basename”>
    <value>messages</value>
    </property>
    </bean>

    thanks,
    deno

    #268591 Reply

    Riyad Kalla
    Member

    Hmm, what does your hibernate.cfg.xml file look like?

    #268600 Reply

    denov
    Member

    @support-rkalla wrote:

    Hmm, what does your hibernate.cfg.xml file look like?

    
    
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    
        <session-factory>
            <property name="connection.username">dvichas</property>
            <property name="connection.url">
                jdbc:sqlserver://localhost:1433;database=auction
            </property>
            <property name="dialect">
                org.hibernate.dialect.SQLServerDialect
            </property>
            <property name="myeclipse.connection.profile">
                localhost
            </property>
            <property name="connection.password">dvichas</property>
            <property name="connection.driver_class">
                com.microsoft.sqlserver.jdbc.SQLServerDriver
            </property>
            <property name="hibernate.dialect">
                org.hibernate.dialect.SQLServerDialect
            </property>
                    
            <mapping resource="com/autoreturn/model/Vehicle.hbm.xml" />
            <mapping resource="com/autoreturn/model/AttendeeType.hbm.xml" />
            <mapping resource="com/autoreturn/model/AttendeeStatus.hbm.xml" />
            <mapping resource="com/autoreturn/model/PaymentType.hbm.xml" />
            <mapping resource="com/autoreturn/model/Deposit.hbm.xml" />
            <mapping resource="com/autoreturn/model/Purchase.hbm.xml" />
            <mapping resource="com/autoreturn/model/LicenseType.hbm.xml" />
            <mapping resource="com/autoreturn/model/Address.hbm.xml" />
            <mapping resource="com/autoreturn/model/AuctionAttendees.hbm.xml" />
            <mapping resource="com/autoreturn/model/Attendee.hbm.xml" />
            <mapping resource="com/autoreturn/model/Auction.hbm.xml" />
    
        </session-factory>
    
    </hibernate-configuration>
    
    
    
    #268604 Reply

    Haris Peco
    Member

    The OpenSessionInViewInterceptor will by default not flush the Hibernate Session.You can try set the “flushMode” of this interceptor to FLUSH_AUTO
    (property flushMode in interceptor’s bean)

    Regards,

    #268605 Reply

    denov
    Member

    @support-snpe wrote:

    The OpenSessionInViewInterceptor will by default not flush the Hibernate Session.You can try set the “flushMode” of this interceptor to FLUSH_AUTO
    (property flushMode in interceptor’s bean)

    this doesn’t work. i added to my OpenSessionInViewInterceptor bean

    
            <property name="flushModeName">
                <value>FLUSH_AUTO</value>
            </property> 
    

    on another a project we didn’t have to set the flush mode for OpenSessionInViewInterceptor. another developer (that’s no longer here) did the initial spring and hibernate setup. we aren’t using a transaction proxy either. the only real difference i see is that we use session.save() vs. getHibernateTemplate().save().

    #268606 Reply

    denov
    Member

    one other thing;

    putting a getHibernateTemplate().flush() after the getHibernateTemplate().save(transientInstance) statement does nothing.

    the hibernate reserve engineering tool created a HibernateSessionFactory class. Does the reserve engineering tool put this to use anywhere? i’m i suppose to be using it?

    #268616 Reply

    Riyad Kalla
    Member

    <property name=”flushModeName”>
    <value>FLUSH_AUTO</value>
    </property>

    I believe the property is “flushMode”, try that and see if it works.

    #268620 Reply

    denov
    Member

    @support-rkalla wrote:

    <property name=”flushModeName”>
    <value>FLUSH_AUTO</value>
    </property>

    I believe the property is “flushMode”, try that and see if it works.

    what would be the correct value for this be?

    #268621 Reply

    denov
    Member
    #268634 Reply

    Haris Peco
    Member

    Deno,

    FlushMode is your problem.You can try with flushMode (it is possible that spring have some bug) or override OpenSessionInViewInterceptor and set flush mode in the getSession or openSession method.You can try hibernate operations using HibernateTemplaet#execute directly as well.

    Regards

    #268859 Reply

    denov
    Member

    ok i figured it out. i’ve seen so many posts about setting the flush Mode in the OpenSessionInViewInterceptor and none of them really made much sense to why. i personally think this is just wrong. to resolve this problem you need to wrap all hibernate calls in a transaction. i found this blog after i figure it out from reading spring and hibernate docs.

    http://nerdnotes.wordpress.com/2007/03/30/basic-transactions-in-spring-using-transactionproxyfactorybean/ it shows one one to do this. my method is a little different.

    
        <!--  ************** Hibernate **************  -->
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
        </bean>
    
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </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>
        
    
        <!--  ************** Managers **************  -->
    
        <bean id="AuctionManager" parent="transactionProxyTemplate">
               <property name="target">
                   <bean class="com.autoreturn.manager.AuctionManagerImpl">
                    <constructor-arg index="0" ref="AuctionDAO" />
                </bean>
            </property>
         </bean> 
    

    also see http://www.springframework.org/docs/api/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.html

    #268864 Reply

    Riyad Kalla
    Member

    denov,
    Thank you *very* much for posting all your findings for other folks running into the same issue. I personally haven’t run into this before so I was at a loss as to what had to be done so this information is very helpful.

Viewing 12 posts - 1 through 12 (of 12 total)
Reply To: Write operations are not allowed in read-only mode

You must be logged in to post in the forum log in