hyljava

          導(dǎo)航

          <2012年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          統(tǒng)計(jì)

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          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或者清除事務(wù)定義中的readOnly標(biāo)記。

           

          錯(cuò)誤原因:
                    OpenSessionInViewFilter在getSession的時(shí)候,會(huì)把獲取回來的session的flush mode 設(shè)為FlushMode.NEVER。然后把該sessionFactory綁定到TransactionSynchronizationManager,使request的整個(gè)過程都使用同一個(gè)session,在請求過后再接除該sessionFactory的綁定,最后closeSessionIfNecessary根據(jù)該session是否已和transaction綁定來決定是否關(guān)閉session。在這個(gè)過程中,若HibernateTemplate 發(fā)現(xiàn)自當(dāng)前session有不是readOnly的transaction,就會(huì)獲取到FlushMode.AUTO Session,使方法擁有寫權(quán)限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER轉(zhuǎn)為Flush.AUTO,擁有insert,update,delete操作權(quán)限,如果沒有transaction,并且沒有另外人為地設(shè)flush model的話,則doFilter的整個(gè)過程都是Flush.NEVER。所以受transaction(聲明式的事務(wù))保護(hù)的方法有寫權(quán)限,沒受保護(hù)的則沒有。

          解決方法:

          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"/> //這些操作會(huì)無效
             <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

          好  回復(fù)  更多評論   

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

          @好
          好好好  回復(fù)  更多評論   

          主站蜘蛛池模板: 晋中市| 精河县| 万载县| 封开县| 辽宁省| 池州市| 娄底市| 福泉市| 巩留县| 广安市| 正安县| 东安县| 资兴市| 绥化市| 乐平市| 固阳县| 绥芬河市| 河北省| 甘肃省| 石首市| 大同市| 合阳县| 永丰县| 乌鲁木齐县| 洛川县| 德清县| 炉霍县| 阿鲁科尔沁旗| 平安县| 五河县| 清远市| 焉耆| 南丹县| 昌图县| 泉州市| 申扎县| 越西县| 丹寨县| 马边| 龙里县| 瓮安县|