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>