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?
- Sessionfactory sessionfactory = lookupsessionfactory (request);
- Session session = null;
- Boolean participate = false;
- if (Issinglesession ()) {
- //single session mode
- if (Transactionsynchronizationmanager.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 (sessionfactory);
- Transactionsynchronizationmanager.bindresource (Sessionfactory, new Sessionholder (session));
- }
- } Else {
- //deferred close mode
- if (sessionfactoryutils.isdeferredcloseactive (sessionfactory)) {
- Do not modify deferred Close:just set the participate flag.
- participate = true;
- } Else {
- Sessionfactoryutils.initdeferredclose (sessionfactory);
- }
- }
- try {
- Filterchain.dofilter (request, response);
- } finally {
- if (!participate) {
- if (issinglesession ()) {
- //single session mode
- Transactionsynchronizationmanager.unbindresource (sessionfactory);
- Logger.debug ("Closing single Hibernate Session in Opensessioninviewfilter");
- CloseSession (session, Sessionfactory);
- }Else {
- //deferred close mode
- Sessionfactoryutils.processdeferredclose (sessionfactory);
- }
- }
- }
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?
- <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>
<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