could not initialize proxy - the owning Session was closed

 

当使用spring+hibernate+struts2的时候,会遇到此问题。
 
大概异常描述 :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
org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:910)
org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:614)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:612)
hibernate.dao.HibernateDAO.saveOrUpdate(HibernateDAO.java:44)。。。。。。。。。。。。。
 
解决办法:
       1:在web.xml里加上OpenSessionInViewFilter的filter。
<!-- OpenSessionInViewFilter--> 
    <filter> 
        <filter-name> OpenSessionInViewFilter </filter-name> 
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
    </filter> 
    <filter-mapping> 
        <filter-name> OpenSessionInViewFilter </filter-name> 
        <url-pattern>/*</url-pattern> 
</filter-mapping>
 
       2.配置事务
     事务方式1:
       <bean name="transactionAttributesSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
              <property name="properties">
                     <props>
                            <prop key="get*">PROPAGATION_REQUIRED</prop>
                            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                            <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                            <prop key="create*">PROPAGATION_REQUIRED</prop>
                            <prop key="update*">PROPAGATION_REQUIRED</prop>
                            <prop key="save*">PROPAGATION_REQUIRED</prop>
                            <prop key="submit*">PROPAGATION_REQUIRED</prop>
                            <prop key="delete*">PROPAGATION_REQUIRED</prop>
                            <prop key="remove*">PROPAGATION_REQUIRED</prop>
                     </props>
              </property>
       </bean>
      
       <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
              <property name="transactionManager">
                     <ref bean="transactionManager"/>
              </property>
              <property name="transactionAttributeSource">
                     <ref bean="transactionAttributesSource"/>
              </property>
       </bean>
 
       <bean id="autoTxProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
              <property name="interceptorNames">
                     <list>
                            <idref local="transactionInterceptor"/>
                     </list>
              </property>
              <property name="beanNames">
                            <value>*Dao</value>
              </property>
       </bean>
     事务方式2:   
       <aop:config proxy-target-class="true"> 
        <aop:pointcut id="contractService" 
            expression="execution(* com.richserc.contract.service..*ContractTypeService.*(..))"/> 
        <aop:pointcut id="contractWebService" 
            expression="execution(* com.richserc.contract.web..*ContractTypeAction.*(..))"/> 
           
        <aop:advisor advice-ref="txAdvice" pointcut-ref="contractService" /> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="contractWebService" />
    </aop:config>
      
       <!-- 基本事务定义,使用transactionManager作事务管理
       <tx:advice id="txAdvice" transaction-manager="transactionManager">
              <tx:attributes>
                     <tx:method name="save*"/> 
            <tx:method name="update*"/> 
            <tx:method name="remove*"/>
            <tx:method name="*" read-only="true"/> 
              </tx:attributes>
       </tx:advice>
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值