Hibernate批量更新和批量刪除

2021-08-25 04:05:51 字數 3682 閱讀 7581

在最近的銀行系統中多處涉及到批量操作問題,起初用hibernate的更新用法沒太注意,開發的時候由於資料量少,看不出有什麼效能問題,到後來造大量資料測試的時候,發現反應超慢,後倆經過仔細分析考慮,採用了儲存過程的方式來解決,果然系統反應快了很多,以下就是解決問題的過程,以客戶資訊表customers表做測試。

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

tx=session.begintransaction();

iteratorcustomers=session.find("fromcustomercwherec.age>0").iterator();

while(customers.hasnext())

tx.commit();

session.close();

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

updatecustomerssetage=?….whereid=i;

updatecustomerssetage=?….whereid=j;

updatecustomerssetage=?….whereid=k;

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

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

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

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

tx=session.begintransaction();

iteratorcustomers=session.find("fromcustomercwherec.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語句:

updatecustomerssetage

age=age+1whereage>0;

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

tx=session.begintransaction();

connectioncon=session.connection();

preparedstatementstmt=con.preparestatement("updatecustomerssetage

age=age+1"

+"whereage>0");

stmt.executeupdate();

tx.commit();

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

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

createorreplaceprocedurebatchupdatecustomer(p_ageinnumber)as

begin

updatecustomerssetage

age=age+1whereage>p_age;

end;

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

tx=session.begintransaction();

connectioncon=session.connection();

stringprocedure="";

callablestatementcstmt=con.preparecall(procedure);

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

cstmt.executeupdate();

tx.commit();

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

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

session.delete("fromcustomercwherec.age>0");

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

deletefromcustomerswhereage>0;

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

select*fromcustomerswhereage>0;

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

deletefromcustomerswhereid=i;

deletefromcustomerswhereid=j;

deletefromcustomerswhereid=k;

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

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

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

在Hibernate中處理批量更新和批量刪除

page load marshal data pump 批量更新是指在乙個事務中更新大批量資料,批量刪除是指在乙個事務中刪除大批量資料。以下程式直接通過hibernate api批量更新customers表中年齡大於零的所有記錄的age欄位 tx session.begintransaction i...

在Hibernate應用中批量更新和批量刪除

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

Hibernate 資料的批量插入 更新和刪除

hibernate完全以物件導向的方式來運算元據庫,當程式裡以物件導向的方式操作持久化物件時,將被自動轉換為對資料庫的操作。例如呼叫session的delete 方法來刪除持久化物件,hibernate將負責刪除對應的資料記錄 當執行持久化物件的set方法時,hibernate將自動轉換為對應的up...