真善美

          勇于承擔(dān)責(zé)任; 如果你做不到,要提前跟別人說(shuō)明,不要等到事情發(fā)生后再解釋。

            BlogJava :: 首頁(yè) ::  :: 聯(lián)系 :: 聚合  :: 管理 ::
            55 隨筆 :: 0 文章 :: 29 評(píng)論 :: 0 Trackbacks

          web.xml原始配置:
           <!-- 過濾spring中對(duì)于hibernate的session關(guān)閉管理 -->
           <filter>
            <filter-name>hibernateFilter</filter-name>
            <filter-class>
             org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
            </filter-class>
           </filter>
          自己寫的serviceImpl.java文件中的保存更新方法(我所出現(xiàn)問題的位置是:多行提交的方法),在運(yùn)行時(shí)總報(bào)錯(cuò)。如下:

          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
          后來(lái)在網(wǎng)上狂搜解決方案,將web.xml文件改為如下:
           <!-- 過濾spring中對(duì)于hibernate的session關(guān)閉管理 -->
           <filter>
               <filter-name>hibernateFilter</filter-name>
                  <filter-class>
                      org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
                  </filter-class>
               <init-param>       
                  <param-name>singleSession</param-name> 
                  <param-value>false</param-value>    
               </init-param>
           </filter>
          上面的異常解決了,但又報(bào)出新的異常,如下:
          org.hibernate.HibernateException: Illegal attempt to associate a collection
          with two open sessions
          解決這個(gè)問題的辦法就是要把singleSession的值改為true
          <init-param>       
                  <param-name>singleSession</param-name> 
                  <param-value>true</param-value>   
          </init-param>

          我無(wú)奈了,這兩個(gè)錯(cuò)誤就好像是相互的,只能解決一個(gè)。。。
          從網(wǎng)上搜。。還真我出現(xiàn)的這咱情況。。結(jié)果如下:
          說(shuō)明一下Open Session in View的作用,就是允許在每次的整個(gè)request的過程中使用同一個(gè)hibernate session,可以在這個(gè)request任何時(shí)期lazy loading數(shù)據(jù)。
          如果是singleSession=false的話,就不會(huì)在每次的整個(gè)request的過程中使用同一個(gè)hibernate session,而是每個(gè)數(shù)據(jù)訪問都會(huì)產(chǎn)生各自的seesion,等于沒有Open Session in View。
          OpenSessionInViewFilter默認(rèn)是不會(huì)對(duì)session 進(jìn)行flush的,并且flush mode 是 never
          代碼:
              protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
                 Session session = SessionFactoryUtils.getSession(sessionFactory, true);
                 session.setFlushMode(FlushMode.NEVER);
                 return session;
              }
          看getSession的方式就知道,把flush mode 設(shè)為FlushMode.NEVER,這樣就算是commit的時(shí)候也不會(huì)session flush,
          如果想在完成request過程中更新數(shù)據(jù)的話, 那就需要先把flush model設(shè)為FlushMode.AUTO,再在更新完數(shù)據(jù)后flush.

          OpenSessionInView默認(rèn)的FlushMode為
          代碼:

          FlushMode.NEVER


          可以采用在寫保存更新刪除代碼的時(shí)候手動(dòng)更改FlushMode
          代碼:

                   this.getHibernateTemplate().execute(new HibernateCallback() {
                       public Object doInHibernate(Session session) throws HibernateException {
                           session.setFlushMode(FlushMode.AUTO);
                           session.save(user);
                           session.flush();
                           return null;
                       }
                   });


          但是這樣做太繁瑣了,第二種方式是采用spring的事務(wù)聲明
          代碼:

               <bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
                     abstract="true">
                   <property name="transactionManager" ref="transactionManager"/>
                   <property name="proxyTargetClass" value="true"/>
                   <property name="transactionAttributes">
                       <props> 
                           <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>


          太巧了,我們的框架就采用了這位前輩所說(shuō)的第二種方案,但是為什么我把配置文件改成他說(shuō)的樣式還是不行呢?
          郁悶中驚奇發(fā)現(xiàn),不是我所有的多行提交都出問題,而只是個(gè)別的。經(jīng)過一翻考慮后,確定自己寫的方法體沒有
          問題了,那么就是方法名了,才發(fā)現(xiàn),還真是方法名的問題。
          原來(lái)自己寫的serviceImpl.java文件的方法名要用“load”“save”“add”“update”“remove”這些詞開頭,這個(gè)就好像是通過這個(gè)bean-service.xml文件管理方法名一樣,超出這個(gè)范圍了,hibernate自身的作用就發(fā)揮不出來(lái)了。
          由于自己對(duì)spring,hibernate的了解不深,暫時(shí)先這樣理解。

          posted on 2007-08-28 17:17 真善美 閱讀(9010) 評(píng)論(5)  編輯  收藏 所屬分類: Spring學(xué)習(xí)
          主站蜘蛛池模板: 九龙城区| 乌鲁木齐县| 高碑店市| 颍上县| 土默特左旗| 连城县| 甘肃省| 河东区| 盐池县| 满城县| 鄂托克前旗| 察哈| 林甸县| 鄯善县| 西华县| 高密市| 柳林县| 盐亭县| 英吉沙县| 于田县| 德惠市| 武汉市| 宁波市| 天津市| 西和县| 怀宁县| 松桃| 林甸县| 盐边县| 建始县| 惠来县| 阿拉善盟| 宁陵县| 巴东县| 双牌县| 松溪县| 临猗县| 新龙县| 漳平市| 油尖旺区| 湘乡市|