Opensessioninviewfilter configuration in spring and web. xml

Source: Internet
Author: User

 

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.
Since I do not know much about spring and hibernate, I need to understand it first.

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.