Declarative transactions and opensessioninview

Source: Internet
Author: User

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.

When I was doing a transaction test today, the console output the above error information and found it online. It turned out that I was in Web. xml
Opensessioninview is set in the file. You can remove the setting.
<! -- Opensessioninview -->
<Filter>
<Filter-Name> opensessioninviewfilter </filter-Name>
<Filter-class> org. springframework. Orm. hibernate3.support. opensessioninviewfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> opensessioninviewfilter </filter-Name>
<URL-pattern> *. Action </url-pattern>
</Filter-mapping>
Or add the transaction settings.

<? 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: AOP = "http://www.springframework.org/schema/aop"
Xmlns: Tx = "http://www.springframework.org/schema/tx"
Xsi: schemalocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory" ref = "sessionfactory"> </property>
</Bean>
 
<TX: Advice id = "txadvice" transaction-Manager = "transactionmanager">
<TX: Attributes>
<TX: method name = "create *" propagation = "required"/>
<TX: method name = "Update *" propagation = "required"/>
<TX: method name = "delete *" propagation = "required"/>
<TX: method name = "find *" propagation = "required" Read-Only = "true"/>
<TX: method name = "*" Read-Only = "true"/>
</TX: Attributes>
</TX: Advice>
 
<AOP: config>
<AOP: pointcut id = "daooperation" expression = "execution (* dgut. Ke. Dao. *. * (...)"/>
<AOP: Advisor advice-ref = "txadvice" pointcut-ref = "daooperation"/>
</AOP: config>
 
</Beans>

I also said on the Internet
Although the open session in view looks good, there are actually many side effects. Check the dofilterinternal method code of opensessioninviewfilter. This method is actually called by the dofilter of the parent class. Therefore, we can understand the call process of opensessioninviewfilter: request) -> open session and start transaction-> controller-> View (JSP)-> end transaction and close session.

Everything looks correct, especially when there is no problem during local development and testing, but imagine if a step in the process is blocked, in this period, the connection will remain occupied and will not be released. The most likely blocked step is to write JSP. On the one hand, it may be because the page content is large, response. Write takes a long time, and on the other hand, it may be because the network speed is slow and the transmission time between the server and the user is long. When a large number of such cases occur, there will be insufficient connection pool connections, resulting in a false page death.

Open session in view is a double-edged sword. Please use it with caution when the content on the public network is heavy.

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.