Write operations are not allowed in read-only mode (flushmode. Never/manual) write operations are not allowed

Source: Internet
Author: User

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.

Problem: In read-only mode (flushmode. Never/manual), write operations are not allowed: Change Your session to flushmode. Commit/auto or clear the readonly mark in the transaction definition.

 

Error cause:
        Opensessioninviewfilter sets the flush mode of the retrieved session to flushmode. Never when getsession. Then bind the sessionfactory to transactionsynchronizati.OnmanagerSo that the entire request process uses the same session. After the request is sent, the sessionfactory binding is received.Closesessionifnecessary determines whether to close the session based on whether the session has been bound to transaction. In this process, if the hibernatetemplate
If the current session has a transaction that is not readonly, The flushmode. Auto session is obtained to grant the write permission to the method. That is, if there is a transaction that is not readonly, it can be flush. convert never to flush. auto has the insert, update, and delete permissions. If there is no transaction and there is no other artificially set flush model, the entire process of dofilter is flush. never. Therefore, transaction (Declarative transactions) The protected method has the write permission, but not the protected method.

Solution:

Add in Web. xml configuration
<Filter>
  <Filter-Name> opensessioninviewfilter </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>
  <Init-param>
           <Param-Name> singlesession </param-Name>
           <Param-value> true </param-value>          
       </Init-param>
       <Init-param>
       <Param-Name> flushmode </param-Name>
  <Param-value> auto </param-value>       
       </Init-param>
</Filter>
  // ....

<Filter-mapping>
  <Filter-Name> opensessioninviewfilter </filter-Name>
  <URL-pattern>/* </url-pattern>
</Filter-mapping>

 

If the configuration in beans. XML is assigned to spring for management

 <Bean id = "txmanager"
  Class = "org. springframework. Orm. hibernate3.hibernatetransactionmanaGER ">
  <Property name = "sessionfactory" ref = "sessionfactory"/>
 </Bean>

 <AOP: config>
  <AOP: pointcut id = "bussinessservice"
   Expression = "execution (* COM. Fan. Service. Base. *. * (..)"/>
  <AOP: Advisor pointcut-ref = "bussinessservice"
   Advice-ref = "txadvice"/>
 </AOP: config>

 <TX: Advice id = "txadvice" transaction-Manager = "txmanager">
  <TX: Attributes>
   <TX: method name = "get *" Read-Only = "false" propagation = "not_supported"/>
   <TX: method name = "find *" Read-Only = "false" propagation = "not_supported"/>
   <TX: method name = "Save *" propagation = "required"/> // If you do not configure save update Delete,
   <TX: method name = "Update *" propagation = "required"/>// These operations will be invalid
   <TX: method name = "delete *" propagation = "required"/>
  </TX: Attributes>
 </TX: Advice>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.