Introduction to OpenSessionInViewFilter of spring

Source: Internet
Author: User

Introduction to OpenSessionInViewFilter of spring

Assume that Hibernate manages its sessions through spring in your application. If OpenSessionInViewFilter or OpenSessionInViewInterceptor is not used in your application. The session will be closed after the end of transaction.
If you adopt spring's declarative transaction mode, it will wrap every method of your proxy object (AOP method ). As follows:

Class = "org. springframework. transaction. interceptor. TransactionProxyFactoryBean">



PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED, readOnly









The transaction type PROPAGATION_REQUIRED of the save * method of the target class org. appfuse. service. impl. BaseManager, and PROPAGATION_REQUIRED of the remove * Method
The transaction types of other methods are PROPAGATION_REQUIRED and readOnly.
So it seems to you that the session is closed after you call the bean method named "manager.
If OpenSessionInViewFilter or OpenSessionInViewInterceptor is used in an application, all opened sessions are saved in a thread variable. Passed before the thread exits
OpenSessionInViewFilter or OpenSessionInViewInterceptor disconnect these sessions. Why? This is mainly to implement the delayed Loading Function of Hibernate. Based on one request
A hibernate session principle.

The description of OpenSessionInViewFilter in spring is as follows:
It is a Servlet2.3 filter 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.

This filter is similar to HibernateInterceptor: It is implemented through a thread. Whether it is an application without transactions or an application with business-layer transactions (through HibernateTransactionManager or
JtaTransactionManager. In the latter case, the transaction will automatically use the Session bound by the filter to perform related operations and complete the commit operations according to the actual situation.

Warning if a single HIbernate Session is used during a request in your application, using this filter will generate some problems that were not encountered before. Note that
The Hibernate Session reorganizes the relationships between persistent objects at the beginning of the request. To avoid conflicts with the same object that has been loaded.

Alternatively, we can adjust this filter to the deferred closing mode by specifying "singleSession" = "false. In this way, a single Session is not used during a request. Each data access or transaction-related
All operations use their own sessions (a bit like not using Open sessions in View ). these sessions are registered in the delayed close mode, even if the related operations have been completed in this request.

"One request for one session" is very effective for the first-level cache, but this can cause side effects. For example, during saveOrUpdate or after transaction rollback, although it is as safe as "no Open Session in View.
However, it allows delayed loading.

It searches for the Session factory in the context root of spring web applications. It also supports bean
Name to find the session factory. The default bean name is "sessionFactory", which is used to find the SessionFactory every request to avoid problems caused by the initialization sequence (when ContextLoaderServlet is used
When integrating spring, the spring application context is initialized only after the filter ).

By default, the filter does not synchronize the Hibernate Session. This is because it considers that this work is done through transactions at the business layer. And the FlushMode of HibernateAccessors is FLUSH_EAGER. If you
You want the filter to synchronize sessions after the request is complete. you need to overwrite its closeSession method. In this method, you need to synchronize the session before calling the closing session operation of the parent class. in addition, you need to overwrite its getSession ()
Method. Returns a session whose FlushMode is not the default FlushMode. NEVER. Note that the getSession () and closeSession () methods are called only in single session mode.

In the wiki of myfaces, a subclass of OpenSessionInViewFilter is provided as follows:
Public class OpenSessionInViewFilter extends org. springframework. orm. hibernate3.support. OpenSessionInViewFilter {

/**
* We do a different flushmode than in the codebase
* Here
*/
Protected Session getSession (SessionFactory sessionFactory) throws DataAccessResourceFailureException {
Session session = SessionFactoryUtils. getSession (sessionFactory, true );
Session. setFlushMode (FlushMode. COMMIT );
Return session;
}
/**
* We do an explicit flush here just in case
* We do not have an automatic flush
*/
Protected void closeSession (Session session, SessionFactory factory ){
Session. flush ();
Super. closeSession (session, factory );
}
}

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.