Java世界

          學(xué)習(xí)筆記

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          天籟村

          新華網(wǎng)

          雅虎

          最新評論

          OpenSessionInViewFilter配置

          最近在配置 Structs, Spring 和Hibernate整合的問題:

          開啟OpenSessionInViewFilter來阻止延遲加載的錯誤的時候拋出了這個異常:

              org.springframework.dao.InvalidDataAccessApiUsageException錯誤
          但是在我們開啟OpenSessionInViewFilter這個過濾器的時候FlushMode就已經(jīng)被默認(rèn)設(shè)置為了MANUAL!

          如果FlushMode是MANUAL或NEVEL,在操作過程中 hibernate會將事務(wù)設(shè)置為readonly,所以在增加、刪除或修改操作過程中會出現(xiàn)如下錯誤:

          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;

           


          解決辦法1:

              直接修改OpenSessionInViewFilter過濾器的配置,配置過濾器的時候配置就是在一般的配置里面加上下面藍(lán)色部分就可以了,直接指定flushMode的配置就OK了:

          下面是配置文件:(web.xml)

          <filter>
          <filter-name>OpenSessionInViewFilter</filter-name>
          <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
          <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>

           

          解決方法2:

              就是配置事務(wù)的邊界,在你方法的執(zhí)行時配置事務(wù)邊界!

          下面是sessionFactor.xml配置:

          <!-- 事務(wù)的配置 -->
          <!-- sessionFactory 為自己配置 sessionFactory 的 bean-->
          <bean id="txManager"
          class
          ="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory" />
          </bean>

          <aop:config>
          <!-- execution(public * *.*.*..*.*(..)) 為自己項(xiàng)目中操作數(shù)據(jù)庫中的方法 -->
          <aop:pointcut id="**" expression="execution(public * *.*.*..*.*(..))" />
          <aop:advisor pointcut-ref="**"
          advice-ref
          ="txAdvice" />
          </aop:config>

          <tx:advice id="txAdvice" transaction-manager="txManager">
          <tx:attributes>
          <!-- name 為 方法名 -->
          <tx:method name="**" read-only="true" />
          <tx:method name="**" propagation="REQUIRED"/>
          </tx:attributes>
          </tx:advice>

           

          下面是總結(jié):

          原理:因?yàn)榕渲胦penSessionInView時,啟動后他默認(rèn)是給沒有配置事務(wù)邊界的方法都默認(rèn)為只讀的,所以在插入數(shù)據(jù)時就會報(bào)上面的錯

          posted on 2012-03-27 09:30 Rabbit 閱讀(412) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 遂宁市| 灵武市| 乌苏市| 阿图什市| 贵溪市| 阿拉善右旗| 南川市| 冀州市| 荥经县| 安泽县| 焦作市| 西华县| 美姑县| 信宜市| 成武县| 增城市| 海盐县| 涟水县| 乡宁县| 泾源县| 孝感市| 汕尾市| 临安市| 甘泉县| 连城县| 新闻| 叙永县| 鄂托克旗| 洪雅县| 昌乐县| 昆明市| 沁源县| 枣阳市| 绥德县| 万全县| 建始县| 日照市| 晴隆县| 古蔺县| 揭阳市| 瑞金市|