Functions of opensessioninviewfilter

Source: Internet
Author: User

Functions of opensessioninviewfilter

Spring solves the issue of closing and enabling hibernate sessions for us.
Hibernate allows delayed loading of associated objects and attributes, but must ensure that the delayed loading operation is limited to the same hibernate session. If the service layer returns a domain object with the delayed Loading Function enabled to the web layer, when the web layer accesses the data that requires delayed loading, the hibernate session for the domain object loading has been disabled, these causes access exceptions for delayed data loading.

(Eg: org. hibernate. lazyinitializationexception :( lazyinitializationexception. Java: 42)
-Failed to lazily initialize a collection of Role: CN. easyjava. Bean. Product. producttype. childtypes, no session or session was closed
Org. hibernate. lazyinitializationexception: failed to lazily initialize a collection of Role: CN. easyjava. Bean. Product. producttype. childtypes, no session or session was closed ).

It is used to bind a hibernate session to the thread corresponding to a complete request process. The purpose is to implement the "open session in view" mode. For example, it allows delayed loading of the desired objects after the transaction is committed.

The opensessioninviewfilter provided by spring solves this problem well. The main function of opensessioninviewfilter is to bind a hibernate session to the thread corresponding to a complete request process. The purpose is to implement the "open session in view" mode. For example, it allows delayed loading of the desired objects after the transaction is committed.
The opensessioninviewfilter filters bind the hibernate session to the request thread, which is automatically detected by the spring Transaction Manager. Therefore, opensessioninviewfilter is applicable to the service layer's transaction management environment using hibernatetransactionmanager or jtatransactionmanager. It can also be used in non-transaction read-only data operations.

<Filter>
<Filter-Name> spring opensessioninviewfilter </filter-Name>
<Filter-class> org. springframework. Orm. hibernate3.support. opensessioninviewfilter </filter-class>
<Init-param>

<! --
Specify the name of org. springframework. Orm. hibernate3.localsessionfactorybean in the spring configuration file. The default value is sessionfactory.
If the name of localsessionfactorybean in spring is not sessionfactory, you must specify this parameter. Otherwise, the exception of sessionfactory cannot be found.
-->
<Param-Name> sessionfactorybean </param-Name>
<Param-value> sessionfactory </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> spring opensessioninviewfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sunsea08/archive/2009/09/12/4545186.aspx

------------------------------------------------- Solution to error 1 ----------------------------------------------------------------------------------------

Original web. xml configuration:
<! -- Filter out hibernate session close management in spring -->
<Filter>
<Filter-Name> hibernatefilter </filter-Name>
<Filter-class>
Org. springframework. Orm. hibernate3.support. opensessioninviewfilter
</Filter-class>
</Filter>
In the self-written serviceimpl. Java file, the update method is saved (The problem occurs in my location: multiline commit method). An error is reported during runtime. As follows:

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
Then I searched for a solution on the Internet and changed the web. xml file to the following:
<! -- Filter out hibernate session close management in spring -->
<Filter>
<Filter-Name> hibernatefilter </filter-Name>
<Filter-class>
Org. springframework. Orm. hibernate3.support. opensessioninviewfilter
</Filter-class>
<Init-param>
<Param-Name> singlesession </param-Name>
<Param-value> false </param-value>
</Init-param>
</Filter>
The above exception is solved, but a new exception is reported as follows:
Org. hibernate. hibernateexception: illegal attempt to associate a collection
With two open sessions
To solve this problem, change the value of singlesession to true.
<Init-param>
<Param-Name> singlesession </param-Name>
<Param-value> true </param-value>
</Init-param>

I am helpless. These two errors are like mutual errors. Only one of them can be solved...
Search from the Internet .. This is my case .. The result is as follows:
The purpose of the open session in view is to allow the same hibernate session to be used throughout each request. The lazy loading data can be requested at any time.
If singlesession is set to false, the same hibernate session will not be used during each request. Instead, each data access will generate a separate seesion, which means no open session in view.
Opensessioninviewfilter does not flush sessions by default, and the flush mode is never.
Code:
Protected session getsession (sessionfactory) throws dataaccessresourcefailureexception {
Session session = sessionfactoryutils. getsession (sessionfactory, true );
Session. setflushmode (flushmode. Never );
Return session;
}
You can see the getsession method and set the flush mode to flushmode. Never, so that even when the commit is used, the session will not be flush,
If you want to update the data during the request, you need to set the flush model to flushmode. Auto first, and then flush after the data is updated.

The default flushmode of opensessioninview is
Code:

Flushmode. Never

You can manually change the flushmode when writing, saving, updating, and deleting code.
Code:

This.gethibernatetemplate(cmd.exe cute (New hibernatecallback (){
Public object doinhibernate (session) throws hibernateexception {
Session. setflushmode (flushmode. Auto );
Session. Save (User );
Session. Flush ();
Return NULL;
}
});

However, this is too cumbersome. The second method is to use the Spring transaction statement.
Code:

<Bean id = "basetransaction" class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean"
Abstract = "true">
<Property name = "transactionmanager" ref = "transactionmanager"/>
<Property name = "proxytargetclass" value = "true"/>
<Property name = "transactionattributes">
<Props>
<Prop key = "load *"> propagation_required, readonly </prop>
<Prop key = "Save *"> propagation_required </prop>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "Remove *"> propagation_required </prop>
</Props>
</Property>
</Bean>
Code:

<Bean id = "userservice" parent = "basetransaction">
<Property name = "target">
<Bean class = "com. phopesoft. Security. Service. impl. userserviceimpl"/>
</Property>
</Bean>

Coincidentally, our framework adopted the second scheme that the predecessor said. But why can't I change the configuration file to the style he said?
I was surprised to find that not all of my multi-line submissions went wrong, but only some of them. After consideration, make sure that the method body you have written is not
If the problem occurs, it is the method name.
Previously written serviceimpl. the method name of the Java file starts with the words "LOAD" "save" "add" "Update" "Remove", which is like managing the method name through this bean-service.xml file, beyond this scope, the role of hibernate itself will not be able to play.

----------------------------------------------------- Solution to the error 2 ------------------------------------------------------------------------------

Use contextloaderlistener and contextloaderplugin at the same time. Do not add applicationcontext. XML in contextloaderplugin, as long as you add actIon-servlet.xml, opensessioninview takes effect.

Because the object saved by contextloaderlistener is key webapplicationcontext. root_web_application_context_attribute! The contextloaderplugin stores the attrname and webapplicationcontext of the key. the value of root_web_application_context_attribute is different, while opensessioninviewfilter is from webapplicationcontext. root_web_application_context_attribute obtains spring configuration information. if XML is configured as a plug-in, opensessioninviewfilter cannot obtain the spring configuration information, and opensessioninviewfilter may fail.

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.