hyljava

          Write operations are not allowed in read-only mode 只讀模式下(FlushMode.NEVER/MANUAL)寫操作不允

          Write operations are not allowed in read-only mode 只讀模式下(FlushMode.NEVER/MANUAL)寫操作不允

          org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

          問題只讀模式下(FlushMode.NEVER/MANUAL)寫操作不被允許:把你的Session改成FlushMode.COMMIT/AUTO或者清除事務定義中的readOnly標記。

           

          錯誤原因:
                    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(聲明式的事務)保護的方法有寫權限,沒受保護的則沒有。

          解決方法:

          web.xml配置里添加
          <filter>
             <filter-name>OpenSessionInViewFilter</filter-name>
             <filter-class>
              org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
             </filter-class>
             <init-param>
              <param-name>sessionFactoryBeanName</param-name>
              <param-value>sessionFactory</param-value>
             </init-param>
             <init-param>
                      <param-name>singleSession</param-name>
                      <param-value>true</param-value>           
                  </init-param>
                  <init-param>
                  <param-name> flushMode </param-name>
             <param-value>AUTO </param-value>        
                  </init-param>
          </filter>
            //   。。。。


          <filter-mapping>
             <filter-name>OpenSessionInViewFilter</filter-name>
             <url-pattern>/*</url-pattern>
          </filter-mapping>

           

          如果在交給spring 管理的情況下,在beans.xml 里的配置

           <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
           </bean>

           <aop:config>
            <aop:pointcut id="bussinessService"
             expression="execution(* com.fan.service.base.*.*(..))" />
            <aop:advisor pointcut-ref="bussinessService"
             advice-ref="txAdvice" />
           </aop:config>

           <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
             <tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED"/>
             <tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED"/>
             <tx:method name="save*" propagation="REQUIRED"/> // 如果不把save update delete都配置上,
             <tx:method name="update*" propagation="REQUIRED"/> //這些操作會無效
             <tx:method name="delete*" propagation="REQUIRED"/>
            </tx:attributes>
           </tx:advice>

          posted on 2012-09-05 11:57 何云隆 閱讀(31499) 評論(2)  編輯  收藏 所屬分類: HibernatespringStruts2

          評論

          # re: Write operations are not allowed in read-only mode 只讀模式下(FlushMode.NEVER/MANUAL)寫操作不允 2016-08-08 17:47

          好  回復  更多評論   

          # re: Write operations are not allowed in read-only mode 只讀模式下(FlushMode.NEVER/MANUAL)寫操作不允 2016-08-08 17:47 好好

          @好
          好好好  回復  更多評論   

          主站蜘蛛池模板: 页游| 威宁| 林周县| 湘潭市| 伊春市| 金堂县| 喜德县| 房山区| 尼勒克县| 隆尧县| 临潭县| 区。| 都江堰市| 济宁市| 永新县| 且末县| 韶关市| 襄汾县| 深圳市| 青龙| 井研县| 门源| 固镇县| 江阴市| 蓬溪县| 元氏县| 阜阳市| 谢通门县| 河源市| 山丹县| 正镶白旗| 南安市| 东乡县| 施甸县| 海口市| 舞钢市| 大田县| 娄烦县| 玛曲县| 台前县| 商都县|