Hibernate事務中更新大批量資料的處理

2021-08-31 12:55:03 字數 3344 閱讀 6750

批量更新是指在乙個事務中更新大批量資料,批量刪除是指在乙個事務中刪除大批量資料。以下程式直接通過hibernate api批量更新customers表中年齡大於零的所有記錄的age欄位:

tx = session.begintransaction();

iterator customers=session.find("from customer c where c.age>0").iterator();

while(customers.hasnext())

tx.commit();

session.close();

如果customers表中有1萬條年齡大於零的記錄,那麼session的find()方法會一下子載入1萬個customer物件到記憶體。當執行tx.commit()方法時,會清理快取,hibernate執行1萬條更新customers表的update語句:

update customers set age=? …. where id=i;

update customers set age=? …. where id=j;

……update customers set age=? …. where id=k;

以上批量更新方式有兩個缺點:

(1) 占用大量記憶體,必須把1萬個customer物件先載入到記憶體,然後一一更新它們。

(2) 執行的update語句的數目太多,每個update語句只能更新乙個customer物件,必須通過1萬條update語句才能更新一萬個customer物件,頻繁的訪問資料庫,會大大降低應用的效能。

為了迅速釋放1萬個customer物件占用的記憶體,可以在更新每個customer物件後,就呼叫session的evict()方法立即釋放它的記憶體:

tx = session.begintransaction();

iterator customers=session.find("from customer c where c.age>0").iterator();

while(customers.hasnext())

tx.commit();

session.close();

在以上程式中,修改了乙個customer物件的age屬性後,就立即呼叫session的flush()方法和evict()方法,flush()方法使hibernate立刻根據這個customer物件的狀態變化同步更新資料庫,從而立即執行相關的update語句;evict()方法用於把這個customer物件從快取中清除出去,從而及時釋放它占用的記憶體。

但evict()方法只能稍微提高批量操作的效能,因為不管有沒有使用evict()方法,hibernate都必須執行1萬條update語句,才能更新1萬個customer物件,這是影響批量操作效能的重要因素。假如hibernate能直接執行如下sql語句:

update customers set age=age+1 where age>0;

那麼以上一條update語句就能更新customers表中的1萬條記錄。但是hibernate並沒有直接提供執行這種update語句的介面。應用程式必須繞過hibernate api,直接通過jdbc api來執行該sql語句:

tx = session.begintransaction();

connection con=session.connection();

preparedstatement stmt=con.preparestatement("update customers set age=age+1 "

+"where age>0 ");

stmt.executeupdate();

tx.commit();

以上程式演示了繞過hibernate api,直接通過jdbc api訪問資料庫的過程。應用程式通過session的connection()方法獲得該session使用的資料庫連線,然後通過它建立preparedstatement物件並執行sql語句。值得注意的是,應用程式仍然通過hibernate的transaction介面來宣告事務邊界。

如果底層資料庫(如oracle)支援儲存過程,也可以通過儲存過程來執行批量更新。儲存過程直接在資料庫中執行,速度更加快。在oracle資料庫中可以定義乙個名為batchupdatecustomer()的儲存過程,**如下:

create or replace procedure batchupdatecustomer(p_age in number) as

begin

update customers set age=age+1 where age>p_age;

end;

以上儲存過程有乙個引數p_age,代表客戶的年齡,應用程式可按照以下方式呼叫儲存過程:

tx = session.begintransaction();

connection con=session.connection();

string procedure = "";

callablestatement cstmt = con.preparecall(procedure);

cstmt.setint(1,0); //把年齡引數設為0

cstmt.executeupdate();

tx.commit();

從上面程式看出,應用程式也必須繞過hibernate api,直接通過jdbc api來呼叫儲存過程。

session的各種過載形式的update()方法都一次只能更新乙個物件,而delete()方法的有些過載形式允許以hql語句作為引數,例如:

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

如果customers表中有1萬條年齡大於零的記錄,那麼以上**能刪除一萬條記錄。但是session的delete()方法並沒有執行以下delete語句:

delete from customers where age>0;

session的delete()方法先通過以下select語句把1萬個customer物件載入到記憶體中:

select * from customers where age>0;

接下來執行一萬條delete語句,逐個刪除customer物件:

delete from customers where id=i;

delete from customers where id=j;

……delete from customers where id=k;

由此可見,直接通過hibernate api進行批量更新和批量刪除都不值得推薦。而直接通過jdbc api執行相關的sql語句或呼叫相關的儲存過程,是批量更新和批量刪除的最佳方式,這兩種方式都有以下優點:

(1) 無需把資料庫中的大批量資料先載入到記憶體中,然後逐個更新或修改它們,因此不會消耗大量記憶體。

(2) 能在一條sql語句中更新或刪除大批量的資料。

hibernate事務簡介

資料庫的事務如果控制不好,很容易導致資料出現髒讀,不可重複讀,幻讀,這樣就會造成資料的混亂,那怎麼弄呢?利用資料庫事務的隔離級別進行控制就可以了 hibernate隔離級別有 1 read uncommitted isolation 2 read committed isolation 4 repe...

hibernate 事務機制

資料庫事務的定義 事務是指一組相互信賴的操作行為。這些操作要麼必須全部成功,要麼必須全部失敗,以保證資料的一致性和完整性。資料庫事務是對現實生活中事務的模擬,它由一組在業務邏輯上相互信賴的sql語句組成。資料庫事務的acid屬性 原子性 atomic 指整個資料庫事務是不可分割的工作單元。原子性確保...

HIbernate 事務特性

1.原子性 atomicity 事務的原子性是指事務中包含的所有操作要麼全做,要麼全不做 all or none 2.一致性 consistency 在事務開始以前,資料庫處於一致性的狀態,事務結束後,資料庫也必須處於一致性狀態。拿銀行轉賬來說,一致性要求事務的執行不應改變a b 兩個賬戶的金額總和...