Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7585613 建立时间:2006年5月29日 |

| |
[Spring]Open Session In View探讨 软件技术, 电脑与网络
lhwork 发表于 2006/7/6 15:00:37 |
当View层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。
OpenSessionInViewInterceptor配置:
500)this.width=500'><beans>
500)this.width=500'> <bean name="openSessionInViewInterceptor"
500)this.width=500'>class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
500)this.width=500'> <property name="sessionFactory">
500)this.width=500'> <ref bean="sessionFactory"/>
500)this.width=500'> </property>
500)this.width=500'> </bean>
500)this.width=500'> <bean id="urlMapping"
500)this.width=500'>class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
500)this.width=500'> <property name="interceptors">
500)this.width=500'> <list>
500)this.width=500'> <ref bean="openSessionInViewInterceptor"/>
500)this.width=500'> </list>
500)this.width=500'> </property>
500)this.width=500'> <property name="mappings">
500)this.width=500'> ...500)this.width=500'> </property>
500)this.width=500'> </bean>
500)this.width=500'>...500)this.width=500'></beans>
OpenSessionInViewFilter配置
500)this.width=500'><web-app>
500)this.width=500'>...500)this.width=500'> <filter>
500)this.width=500'> <filter-name>hibernateFilter</filter-name>
500)this.width=500'> <filter-class>
500)this.width=500'> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter500)this.width=500'> </filter-class>
500)this.width=500'> <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
500)this.width=500'> <init-param>
500)this.width=500'> <param-name>singleSession</param-name>
500)this.width=500'> <param-value>true</param-value>
500)this.width=500'> </init-param>
500)this.width=500'> </filter>
500)this.width=500'>...500)this.width=500'> <filter-mapping>
500)this.width=500'> <filter-name>hibernateFilter</filter-name>
500)this.width=500'> <url-pattern>*.do</url-pattern>
500)this.width=500'> </filter-mapping>
500)this.width=500'>...500)this.width=500'></web-app>
很多人在使用OpenSessionInView过程中提及一个错误:
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
看看OpenSessionInViewFilter里的几个方法:
500)this.width=500'>protected void doFilterInternal(HttpServletRequest request,500)this.width=500'>HttpServletResponse response,FilterChain filterChain)500)this.width=500'>500)this.width=500'>throws ServletException, IOException ...{500)this.width=500'> SessionFactory sessionFactory = lookupSessionFactory();500)this.width=500'> logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");500)this.width=500'> Session session = getSession(sessionFactory);500)this.width=500'> TransactionSynchronizationManager.bindResource(500)this.width=500'> sessionFactory, new SessionHolder(session));500)this.width=500'>500)this.width=500'> try ...{500)this.width=500'> filterChain.doFilter(request, response);500)this.width=500'> }
500)this.width=500'>500)this.width=500'> finally ...{500)this.width=500'> TransactionSynchronizationManager.unbindResource(sessionFactory);500)this.width=500'> logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");500)this.width=500'> closeSession(session, sessionFactory);500)this.width=500'> }
500)this.width=500'>}
500)this.width=500'>500)this.width=500'>protected Session getSession(SessionFactory sessionFactory)500)this.width=500'>500)this.width=500'>throws DataAccessResourceFailureException ...{500)this.width=500'> Session session = SessionFactoryUtils.getSession(sessionFactory, true);500)this.width=500'> session.setFlushMode(FlushMode.NEVER);500)this.width=500'> return session;500)this.width=500'>}
500)this.width=500'>500)this.width=500'>protected void closeSession(Session session, SessionFactory sessionFactory)500)this.width=500'>500)this.width=500'>throws CleanupFailureDataAccessException ...{500)this.width=500'> SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);500)this.width=500'>}
可以看到OpenSessionInViewFilter在getSession的时候,会把获取回来的
session的flush mode
设为FlushMode.NEVER。然后把该sessionFactory绑定到
TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该
sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决
定是否关闭session。在这个过程中,若HibernateTemplate
发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO
Session,使方法拥有写权限。
500)this.width=500'>public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory)500)this.width=500'>500)this.width=500'> throws CleanupFailureDataAccessException ...{500)this.width=500'> if (session == null ||
500)this.width=500'>TransactionSynchronizationManager.hasResource(sessionFactory)) ...{500)this.width=500'> return;500)this.width=500'> }
500)this.width=500'> logger.debug("Closing Hibernate session");500)this.width=500'>500)this.width=500'> try ...{500)this.width=500'> session.close();500)this.width=500'> }
500)this.width=500'>500)this.width=500'> catch (JDBCException ex) ...{500)this.width=500'> // SQLException underneath
500)this.width=500'> throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException());500)this.width=500'> }
500)this.width=500'>500)this.width=500'> catch (HibernateException ex) ...{500)this.width=500'> throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex);500)this.width=500'> }
500)this.width=500'> }
也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为
Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush
model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。
采用spring的事务声明,使方法受transaction控制:
500)this.width=500'> <bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
500)this.width=500'> abstract="true">
500)this.width=500'> <property name="transactionManager" ref="transactionManager"/>
500)this.width=500'> <property name="proxyTargetClass" value="true"/>
500)this.width=500'> <property name="transactionAttributes">
500)this.width=500'> <props>
500)this.width=500'> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
500)this.width=500'> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
500)this.width=500'> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
500)this.width=500'> <prop key="save*">PROPAGATION_REQUIRED</prop>
500)this.width=500'> <prop key="add*">PROPAGATION_REQUIRED</prop>
500)this.width=500'> <prop key="update*">PROPAGATION_REQUIRED</prop>
500)this.width=500'> <prop key="remove*">PROPAGATION_REQUIRED</prop>
500)this.width=500'> </props>
500)this.width=500'> </property>
500)this.width=500'> </bean>
500)this.width=500'>500)this.width=500'> <bean id="userService" parent="baseTransaction">
500)this.width=500'> <property name="target">
500)this.width=500'> <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
500)this.width=500'> </property>
500)this.width=500'> </bean>
对于上例,则以save、add、update、remove开头的方法拥有可写的事务,如果当前有某个方
法,如命名为importExcel(),则因没有transaction而没有写权限,这时若方法内有insert、update、delete操作的
话,则需要手动设置flush model为Flush.AUTO,如:
500)this.width=500'> session.setFlushMode(FlushMode.AUTO);500)this.width=500'> session.save(user);500)this.width=500'> session.flush();
尽管Open Session In
View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代码,这个方法
实际上是被父类的doFilter调用的,因此,我们可以大约了解的OpenSessionInViewFilter调用流程:request(请求)-
>open
session并开始transaction->controller->View(Jsp)->结束transaction并
close session。
一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这
期间connection就一直被占用而不释放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,
另一方面可能是网速慢,服务器与用户间传输时间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。
Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用。 |
|
到底是咋回事啊 软件技术, 电脑与网络
九千岁(游客)发表评论于2008/4/11 23:27:01 |
到底是都不会啊?还是都不屑啊?!
我查了一个钟头的帖子,所有的帖子在Interceptor的配置里面,遇到下面这句,
<property name="mappings">...</property>
一律都略过不写,到底是都不会啊还是代码太简单了都不屑于贴上啊?!那么多千篇一律的代码都贴上了,不会不屑于多贴上很多人都在找的那一部分吧?! |
|
» 1 »
|