簡體   English   中英

hibernate並發線程問題

[英]hibernate concurrency threading issue

我們正在為我們的應用程序使用spring / hibernate ..我有一個cron作業,它選擇新的注釋來更新父母關系,其中注釋可以被另一條評論作為父級.. cron選擇塊中的評論..我的問題是假設我有10條評論.. 1,2,3 ... 10

cron將以塊的形式處理注釋
thread-1(1..5)
線程-2(6-10)

// now in hibernate I'm using 
@PersistenceContext(name = "pu1", type = PersistenceContextType.TRANSACTION)
protected EntityManager em;

所以thread-1從父表中讀取以檢查當前評論塊是否有任何父項。然后它將默認值插入父表。

問題是線程1插入到數據庫父表中的內容對於線程2是不可見的,反之亦然。

// we are using,, but this is not working
commentsService.getEntityManger().flush();

// the only way to see the changes either by adding 
commentsService.getEntityManger().getTransaction().commit() // this will throw exception (transaction is not active)

// OR by adding 
Thread.sleep(3000); //before reading from parent table.

// from logs "select * from parent" for each thread.
// by this time comment 648 was inserted to parent 
2015-02-19 04:54:09,289 DEBUG [pool-2-thread-1] (CommentsServiceImpl.java:679) - Parent Table:: 23424977 for Comment 648
2015-02-19 04:54:09,289 DEBUG [pool-2-thread-1] (CommentsServiceImpl.java:679) - Parent Table:: comment_id: 648 for Comment 648

// by this time comment 649 was inserted to parent
// comment number 649 is inserted to parent table, but thread-2 can't see comment number 648 which is supposed to be available in parent table
2015-02-19 04:54:09,292 DEBUG [pool-2-thread-2] (CommentsServiceImpl.java:679) - Parent Table:: comment_id: 23424977 for Comment 649
2015-02-19 04:54:09,292 DEBUG [pool-2-thread-2] (CommentsServiceImpl.java:679) - Parent Table:: comment_id: 649 for Comment 649

如果T1和T2都處於活動狀態,並且在任一事務中,如果要查看另一個但尚未提交的刷新到數據庫的更改,則需要將事務隔離級別更改為READ_UNCOMMITTED:

http://www.byteslounge.com/tutorials/spring-transaction-isolation-tutorial

暫無
暫無

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

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