OpenSessionInViewFilter(转)

????? 最近工作中发现查询的时候超级慢,一个简单的都得10秒呢,所以看了看后台的SQL显示N多条SQL,想了想可能是表和表之间关系并用了延迟加载是false造成的。去网上找了找发现。
<filter>
??? ??? <filter-name>hibernateFilter</filter-name>
??? ??? <filter-class>
??? ??? ??? org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
??? ??? </filter-class>
??? </filter>

??? <filter-mapping>
??? ??? <filter-name>hibernateFilter</filter-name>
??? ??? <url-pattern>*.action</url-pattern>
??? </filter-mapping>
这样可以实现延迟加载非得设置=false的,这样可以将加载一直到页面显示完。可以将lazy=false去掉了.
原理(转):因为你在jsp上用比如user.class.classname取classname的时候(user和class一对多),session已经关闭了,除非你在action已经把class取出来,在页面上直接解析class.classname,openSessionInView的意思就 是延长session的生命周期,到了jsp层,session依然有效,所以就可以正确读取一对多的多方的属性值了.
但在删除的时候却出现了一个异常:
<clk><nobr style="border-bottom: 1px dotted rgb(102, 0, 255); color: rgb(102, 0, 255); background-color: transparent;" false="" this)="">错误代码:
? org. <clk><nobr style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" false"="" this)"="">springframework</nobr></clk>. dao. <clk><nobr style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" false"="" this)"="">InvalidDataAccessApiUsageException</nobr></clk><clk>: Write <nobr style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" false"="" this)"="">operations are not allowed in read-only mode (FlushMode. NEVER ) - turn your Session into FlushMode. AUTO or remove 'readOnly'<clk>?marker from <nobr style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" false"="" this)"="">transaction definition <nobr style="border-bottom: 1px dotted rgb(102, 0, 255); color: rgb(102, 0, 255); background-color: transparent;" false="" this)="">
<clk><nobr style="border-bottom: 1px dotted rgb(102, 0, 255); color: rgb(102, 0, 255); background-color: transparent;" false="" this)="">
错误原因:
<clk>?<nobr style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" false"="" this)"="">OpenSessionInViewFilter在 getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该 sessionFactory的绑定,最后 closeSessionIfNecessary根 据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。
也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为 Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。

解决办法:
? 采用spring的事务声明,使方法受transaction控制
<bean id="baseTransaction" <br="">class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
? ? ? ? ? abstract="true">
? ? ? ? <property name="transactionManager" ref="transactionManager">
? ? ? ? <property name="proxyTargetClass" value="true">
? ? ? ? <property name="transactionAttributes">
? ? ? ? ? ? <props>
? ? ? ? ? ? ? ? <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
? ? ? ? ? ? ? ? <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
? ? ? ? ? ? ? ? <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>?
??? <bean id="userService" parent="baseTransaction">
? ? ? ? <property name="target">
? ? ? ? ? ? <bean class="com.phopesoft.security.service.impl.UserServiceImpl">
? ? ? ? </property>
? ? </bean> <nobr style="border-bottom: 1px dotted rgb(102, 0, 255); color: rgb(102, 0, 255); background-color: transparent;" false="" this)="">
<clk><nobr style="border-bottom: 1px dotted rgb(102, 0, 255); color: rgb(102, 0, 255); background-color: transparent;" false="" this)=""> <clk><nobr style="border-bottom: 1px dotted rgb(102, 0, 255); color: rgb(102, 0, 255); background-color: transparent;" false="" this)="">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值