Opensessioninviewfilter and Org.springframework.dao.InvalidDataAccessApiUsageException

Source: Internet
Author: User
Tags aop

This article reproduced: Iuranus

Error: Org.springframework.dao.InvalidDataAccessApiUsageException:Write operations is not allowed in read-only mode ( flushmode.never/manual): Turn your Session to Flushmode.commit/auto or remove ' readOnly ' marker from transaction definit Ion.

Engaged in the development of the problem encountered in the online search for a reason, summed up into the following several knowledge points, some excerpts from the network original:

1. Delayed loading:

Hibernate allows associated objects to be loaded lazily, provided that the deferred load operation is guaranteed to be within the same session range. If the service layer returns a domain object that has lazy loading enabled to the view layer, when the view layer accesses data that requires lazy loading, the session that loads the domain object is closed, An Access exception (org.hibernate.LazyInitializationException) that will cause lazy loading of data.

2, Opensessioninviewfilter:

As is known to all, Java class or method naming is famous for its long name, Opensessioninviewfilter is also, as the name implies, it allows us to keep the session open in the view layer. The spring-provided opensessioninviewfilter is used to bind a hibernate session to a line threads the complete request process (the entire request process is the same session, which is unbound after the request is completed). Allows deferred loading of objects required by the view layer after a transaction commits. During the binding process, it is automatically detected by spring's transaction manager, so the Opensessioninviewfilter Applies to an environment where the service tier uses Hibernatetransactionmanager or Jtatransactionmanager for transaction management, and can also be used in non-transactional read-only data operations.

"Opensessioninviewfilter in GetSession, will get back to the session of the flush mode set to Flushmode.never." The sessionfactory is then bound to the Transactionsynchronizationmanager so that the entire request process uses the same session, after which the sessionfactory is unbound, Finally closesessionifnecessary determines whether the session is closed based on whether the session has been bound to the transaction. In this process, if hibernatetemplate found from the current session is not READONLY transaction, will get to the Flushmode.auto Session, so that the method has write permissions. ”

3, the solution:

Config opensessioninviewfilter initial parameters in Web. xml: Singlesession:true, Flushmode:auto

<filter>  <filter-name>OpenSessionInViewFilter</filter-name>  <filter-class> Org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class>  <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>*.action</url-pattern ></filter-mapping>

Or, in the spring configuration file, set the Read-only of the method to false. I use annotations, for the diagram convenience, the method of @transactional (ReadOnly = True) can be removed.

<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"/>     <tx:method name= "update*" propagation= "REQUIRED"/>    <tx:method name= "delete*" propagation= "REQUIRED"/>  </tx:attributes></tx:advice>

  

4. Remark:

"Although the open Session in view looks good, there are many side effects. Looking back at the Opensessioninviewfilter Dofilterinternal method code above, this method is actually called by the Dofilter of the parent class, so We can learn about the Opensessioninviewfilter invocation process: request (requests)->open session and start Transaction->controller->view (JSP)- > End transaction and close session. ”
"Everything looks right, especially if there's no problem with the local development test, but imagine if a step in the process is blocked, then the connection is always occupied and not released." The most likely to be blocked is to write JSP this step, on the one hand may be the page content is large, Response.Write time is long, on the other hand may be slow, the server and the user between the transmission time long. When a large number of such cases occur, there is insufficient connection pool connection, causing the page suspended animation phenomenon. ”
"Open Session in view is a double-edged sword, put on the public web content of large-volume Web site please use caution." ”

Opensessioninviewfilter and Org.springframework.dao.InvalidDataAccessApiUsageException

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.