簡體   English   中英

Hibernate AssertionFailure在不同的線程中

[英]Hibernate AssertionFailure in different Threads

我用一個會話連接到我的數據庫。 我在整個計划中總是使用相同的會話。 我的線程“1”從數據庫中捕獲主數據。 必須允許用戶取消此線程。 因此,如果用戶經常或快速按下取消按鈕(這是我的解釋),則會發生以下錯誤:

ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) 
org.hibernate.AssertionFailure: possible non-threadsafe access to the session

如果我在我的線程“1”完成並嘗試從數據庫加載另一個主數據集后取消我在后台運行的線程“2”,則會發生相同的錯誤。

我在兩個線程中使用相同會話的失敗是什么?

解決這個問題的正確方法是什么?

每個線程都應該從Hibernate會話工廠獲取自己的會話。

並不意味着實現者是線程安全的。 相反,每個線程/事務應從SessionFactory獲取自己的實例。

請參見: Hibernate Session JavaDoc

當你“取消”一個線程時 - 它應該像事務回滾,會話關閉等那樣進行自己的清理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM