Go Java Programmer from Stupid Bird to rookie (83) details of Spring (12) Opensessioninview and usage

Source: Internet
Author: User

First, let's take a look at what is Opensessioninview?

InHibernateUsed inLoadMethod is closed when the data is not actually acquired.Session, and when we really want to get data, it forcesLoadLoad the data, and at this pointSessionis turned off, so an exception occurs. The more typical is theMvcmode, we areMWhen a layer calls the persistence layer for dataload ) session with it closed, and we want to vload Load the data, we want the session open Well, that's what it's called. open session in view 

OpensessioninviewfilterIsSpringProvides a response toHibernateA support class that is primarily meant to be opened when a page request is initiatedHibernateOfSession, has kept thisSessionUntil this request is over, specifically through aFilterTo achieve. BecauseHibernateIntroduced aLazy LoadCharacteristics that make it outhibernatesession periodic objects if you want to pass getterhibernatelazyload exceptionspring introduced this filterhibernate session

First analysis of its source code, you can find that its implementation of the function is actually relatively simple:

[Java]View Plaincopyprint?
  1. Sessionfactory sessionfactory = lookupsessionfactory (request);
  2. Session session = null;
  3. Boolean participate = false;
  4. if (Issinglesession ()) {
  5. //single session mode
  6. if (Transactionsynchronizationmanager.hasresource (sessionfactory)) {
  7. //Do not modify the Session:just set the participate flag.
  8. participate = true;
  9. } Else {
  10. Logger.debug ("Opening single Hibernate Session in Opensessioninviewfilter");
  11. Session = GetSession (sessionfactory);
  12. Transactionsynchronizationmanager.bindresource (Sessionfactory, new Sessionholder (session));
  13. }
  14. } Else {
  15. //deferred close mode
  16. if (sessionfactoryutils.isdeferredcloseactive (sessionfactory)) {
  17. Do not modify deferred Close:just set the participate flag.
  18. participate = true;
  19. } Else {
  20. Sessionfactoryutils.initdeferredclose (sessionfactory);
  21. }
  22. }
  23. try {
  24. Filterchain.dofilter (request, response);
  25. } finally {
  26. if (!participate) {
  27. if (issinglesession ()) {
  28. //single session mode
  29. Transactionsynchronizationmanager.unbindresource (sessionfactory);
  30. Logger.debug ("Closing single Hibernate Session in Opensessioninviewfilter");
  31. CloseSession (session, Sessionfactory);
  32. }Else {
  33. //deferred close mode
  34. Sessionfactoryutils.processdeferredclose (sessionfactory);
  35. }
  36. }
  37. }
Sessionfactory sessionfactory = lookupsessionfactory (request); Session session = Null;boolean participate = false;if (Issinglesession ()) {//single session Modeif (Transactionsynchroniz        Ationmanager.hasresource (sessionfactory)) {//Do not modify the Session:just set the participate flag.participate = true; }else {logger.debug ("Opening single Hibernate Session in Opensessioninviewfilter"); Session = GetSession (sessionfact Ory); Transactionsynchronizationmanager.bindresource (Sessionfactory, new Sessionholder (session));}} else {//deferred close Modeif (sessionfactoryutils.isdeferredcloseactive (sessionfactory)) {//Do is not modify deferred Clo    Se:just Set the participate flag.participate = true;    } else {sessionfactoryutils.initdeferredclose (sessionfactory); }}try {filterchain.dofilter (request, response);} finally {if (!participate) {if (Issinglesession ()) {/ /single session modetransactionsynchronizationmanager.unbindresource (Sessionfactory); Logger.debug ("Closing Single Hibernate Session in Opensessioninviewfilter "); CloseSession (session, sessionfactory);} else {//deferred close modesessionfactoryutils.processdeferredclose (sessionfactory);}}}

In the above code, get sessionfactory first, then get a Session through sessionfactory. The actual Action code is then executed , and the Hibernate Session is closed as appropriate .  The whole idea is quite clear.

Let's take a look at his specific configuration, in fact, it is very simple, directly in Web. XML, the filter is configured on the OK :

[HTML]View Plaincopyprint?
  1. <filter>
  2. <filter-name>opensessioninview</filter-name>
  3. <filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</ Filter-class>
  4. <init-param>
  5. <param-name>sessionfactorybeanname</param-name>
  6. <param-value>sf</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>opensessioninview</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>
<filter><filter-name>openSessionInView</filter-name><filter-class> org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class><init-param>< param-name>sessionfactorybeanname</param-name><param-value>sf</param-value></ Init-param></filter><filter-mapping><filter-name>opensessioninview</filter-name> <url-pattern>/*</url-pattern></filter-mapping>



In the above configuration we should pay attention to the following points:

1, this filter must be configured in the front of the struts filter, because the filter is the "advanced after-out" principle, if you configure the back of struts , your Opensessioninview Filter is done, how to go in the management action of the Turn page AH.

2、Opensessioninviewalso needSessionfactoryOfBeanThe injection, he went to the default to findBeanOfIdForSessionfactory Bean, if the ID of the sessionfactory Bean is not the name, remember to configure the filter with a parameter named Sessionfactorybeanname, set his value to the ID value of your sessionfactory bean.

3, in the use of Opensessioninview must pay attention to, if you do not configure transaction , when using Opensessioninview , he default to the transaction configured to Only-read Read only, so that if you make additions and deletions, he will report a read-only transaction can not be deleted or modified operation. If Opensessioninview is removed, the start boundary of his default transaction is locked on the DAO layer operation, and theDAO layer hibernatetempt Provides start and commit of a transaction

Opensessioninview Side effects of

Once you understand the above questions, you can probably know the side effects of Opensessioninview - serious resource usage, improper configuration, and impact on system performance. With Opensessioninview , If any step is blocked in the process of request issuance and response return, during this period connection will be kept occupied and not released. For example, the page is large, the display needs time or speed is too slow, the server and the user transfer between the time is too long, which will lead to resource consumption, the most direct performance is the connection pool connection is not enough, and the final server can not provide services.

Go Java Programmer from Stupid Bird to rookie (83) details of Spring (12) Opensessioninview and usage

Related Article

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.