Open source are the greatest wealth---WANGPENG
          posts - 46, comments - 11, trackbacks - 0, articles - 0
             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            批量更新是指在一個(gè)事務(wù)中更新大批量數(shù)據(jù),批量刪除是指在一個(gè)事務(wù)中刪除大批量數(shù)據(jù).以下程序直接通過(guò)Hibernate API批量更新CUSTOMERS表中年齡大于零的所有記錄的AGE字段:
            
              tx = session.beginTransaction();
              Iterator customers=session.find("from Customer c where c.age>0").iterator();
              while(customers.hasNext()){
              Customer customer=(Customer)customers.next();
              customer.setAge(customer.getAge()+1);
              }
              tx.commit();
              session.close();
            
            如果CUSTOMERS表中有1萬(wàn)條年齡大于零的記錄,那么Session的find()方法會(huì)一下子加載1萬(wàn)個(gè)Customer對(duì)象到內(nèi)存.當(dāng)執(zhí)行tx.commit()方法時(shí),會(huì)清理緩存,Hibernate執(zhí)行1萬(wàn)條更新CUSTOMERS表的update語(yǔ)句:
            
              update CUSTOMERS set AGE=? …. where ID=i;
              update CUSTOMERS set AGE=? …. where ID=j;
              update CUSTOMERS set AGE=? …. where ID=k;
            
            以上批量更新方式有兩個(gè)缺點(diǎn):
            (1) 占用大量?jī)?nèi)存,必須把1萬(wàn)個(gè)Customer對(duì)象先加載到內(nèi)存,然后一一更新它們.
            (2) 執(zhí)行的update語(yǔ)句的數(shù)目太多,每個(gè)update語(yǔ)句只能更新一個(gè)Customer對(duì)象,必須通過(guò)1萬(wàn)條update語(yǔ)句才能更新一萬(wàn)個(gè)Customer對(duì)象,頻繁的訪問(wèn)數(shù)據(jù)庫(kù),會(huì)大大降低應(yīng)用的性能.
            為了迅速釋放1萬(wàn)個(gè)Customer對(duì)象占用的內(nèi)存,可以在更新每個(gè)Customer對(duì)象后,就調(diào)用Session的evict()方法立即釋放它的內(nèi)存:

              tx = session.beginTransaction();
              Iterator customers=session.find("from Customer c where c.age>0").iterator();
              while(customers.hasNext()){
              Customer customer=(Customer)customers.next();
              customer.setAge(customer.getAge()+1);
              session.flush();
              session.evict(customer);
              }
              tx.commit();
              session.close();

            在以上程序中,修改了一個(gè)Customer對(duì)象的age屬性后,就立即調(diào)用Session的flush()方法和evict()方法,flush()方法使Hibernate立刻根據(jù)這個(gè)Customer對(duì)象的狀態(tài)變化同步更新數(shù)據(jù)庫(kù),從而立即執(zhí)行相關(guān)的update語(yǔ)句;evict()方法用于把這個(gè)Customer對(duì)象從緩存中清除出去,從而及時(shí)釋放它占用的內(nèi)存.
            但evict()方法只能稍微提高批量操作的性能,因?yàn)椴还苡袥](méi)有使用evict()方法,Hibernate都必須執(zhí)行1萬(wàn)條update語(yǔ)句,才能更新1萬(wàn)個(gè)Customer對(duì)象,這是影響批量操作性能的重要因素.假如Hibernate能直接執(zhí)行如下SQL語(yǔ)句:

              update CUSTOMERS set AGE=AGE+1 where AGE>0;

            那么以上一條update語(yǔ)句就能更新CUSTOMERS表中的1萬(wàn)條記錄.但是Hibernate并沒(méi)有直接提供執(zhí)行這種update語(yǔ)句的接口.應(yīng)用程序必須繞過(guò)Hibernate API,直接通過(guò)JDBC API來(lái)執(zhí)行該SQL語(yǔ)句:

              tx = session.beginTransaction();
              Connection con=session.connection();
              PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGE=AGE+1 "
              +"where AGE>0 ");
              stmt.executeUpdate();
              tx.commit();

            以上程序演示了繞過(guò)Hibernate API,直接通過(guò)JDBC API訪問(wèn)數(shù)據(jù)庫(kù)的過(guò)程.應(yīng)用程序通過(guò)Session的connection()方法獲得該Session使用的數(shù)據(jù)庫(kù)連接,然后通過(guò)它創(chuàng)建PreparedStatement對(duì)象并執(zhí)行SQL語(yǔ)句.值得注意的是,應(yīng)用程序仍然通過(guò)Hibernate的Transaction接口來(lái)聲明事務(wù)邊界.
            如果底層數(shù)據(jù)庫(kù)(如Oracle)支持存儲(chǔ)過(guò)程,也可以通過(guò)存儲(chǔ)過(guò)程來(lái)執(zhí)行批量更新.存儲(chǔ)過(guò)程直接在數(shù)據(jù)庫(kù)中運(yùn)行,速度更加快.在Oracle數(shù)據(jù)庫(kù)中可以定義一個(gè)名為batchUpdateCustomer()的存儲(chǔ)過(guò)程,代碼如下:

              create or replace procedure batchUpdateCustomer(p_age in number) as
              begin
              update CUSTOMERS set AGE=AGE+1 where AGE>p_age;
              end;

            以上存儲(chǔ)過(guò)程有一個(gè)參數(shù)p_age,代表客戶的年齡,應(yīng)用程序可按照以下方式調(diào)用存儲(chǔ)過(guò)程:
              tx = session.beginTransaction();
              Connection con=session.connection();
              String procedure = "{call batchUpdateCustomer(?) }";
              CallableStatement cstmt = con.prepareCall(procedure);
              cstmt.setInt(1,0); //把年齡參數(shù)設(shè)為0
              cstmt.executeUpdate();
              tx.commit();

            從上面程序看出,應(yīng)用程序也必須繞過(guò)Hibernate API,直接通過(guò)JDBC API來(lái)調(diào)用存儲(chǔ)過(guò)程.
          Session的各種重載形式的update()方法都一次只能更新一個(gè)對(duì)象,而delete()方法的有些重載形式允許以HQL語(yǔ)句作為參數(shù),例如:

              session.delete("from Customer c where c.age>0");

            如果CUSTOMERS表中有1萬(wàn)條年齡大于零的記錄,那么以上代碼能刪除一萬(wàn)條記錄.但是Session的delete()方法并沒(méi)有執(zhí)行以下delete語(yǔ)句

              delete from CUSTOMERS where AGE>0;

            Session的delete()方法先通過(guò)以下select語(yǔ)句把1萬(wàn)個(gè)Customer對(duì)象加載到內(nèi)存中:

              select * from CUSTOMERS where AGE>0;

            接下來(lái)執(zhí)行一萬(wàn)條delete語(yǔ)句,逐個(gè)刪除Customer對(duì)象:

              delete from CUSTOMERS where ID=i;
              delete from CUSTOMERS where ID=j;
              delete from CUSTOMERS where ID=k;

            由此可見(jiàn),直接通過(guò)Hibernate API進(jìn)行批量更新和批量刪除都不值得推薦.而直接通過(guò)JDBC API執(zhí)行相關(guān)的SQL語(yǔ)句或調(diào)用相關(guān)的存儲(chǔ)過(guò)程,是批量更新和批量刪除的最佳方式,這兩種方式都有以下優(yōu)點(diǎn):

            (1) 無(wú)需把數(shù)據(jù)庫(kù)中的大批量數(shù)據(jù)先加載到內(nèi)存中,然后逐個(gè)更新或修改它們,因此不會(huì)消耗大量?jī)?nèi)存.
            (2) 能在一條SQL語(yǔ)句中更新或刪除大批量的數(shù)據(jù).
           

           

            Hibernate3.0對(duì)批量更新和批量刪除提供了支持,能夠直接執(zhí)行批量更新或批量刪除語(yǔ)句,無(wú)需把被更新或刪除的對(duì)象先加載到內(nèi)存中.以下是通過(guò)Hibernate3.0執(zhí)行批量更新的程序代碼:

          代碼
              Session session = sessionFactory.openSession();    
              Transaction tx = session.beginTransaction();    
              String hqlUpdate = "update Customer set name = :newName where name =:ldName";    
              int updatedEntities = s.createQuery( hqlUpdate )    
              .setString( "newName", newName )    
              .setString( "oldName", oldName )    
              .executeUpdate();    
              tx.commit();    
              session.close();  
            以下是通過(guò)Hibernate3.0執(zhí)行批量刪除的程序代碼:

          代碼
              Session session = sessionFactory.openSession();    
              Transaction tx = session.beginTransaction();    
              String hqlDelete = "delete Customer where name = :ldName";    
              int deletedEntities = s.createQuery( hqlDelete )    
              .setString( "oldName", oldName )    
              .executeUpdate();    
              tx.commit();    
              session.close();  

          主站蜘蛛池模板: 蒙山县| 南通市| 台东市| 沁水县| 武城县| 房产| 乐都县| 岢岚县| 钦州市| 肃北| 通河县| 沐川县| 辽宁省| 莱州市| 黄石市| 陇川县| 勐海县| 宁夏| 阳曲县| 屯门区| 西贡区| 泸水县| 北碚区| 湖州市| 马公市| 云阳县| 南汇区| 西安市| 塔河县| 乃东县| 六安市| 贵南县| 北宁市| 保靖县| 望谟县| 阜平县| 枣庄市| 正阳县| 日照市| 梅州市| 贡觉县|