最近工作中遇到了乙個問題 由於某個批量功能執行時間實在太慢 於是想著使用多執行緒的方法加速 但是執行了之後發現 他們不存在於乙個事務中,想要乙個報錯 全體回滾暫時不明白該怎麼實現
相關偽**
@transactional
public
void
test()
throws interruptedexception })
.start()
;}countdownlatch.
await()
;int i =1/
0;//主線程報錯
}
查閱了相關文件之後 明白了原因
spring相關資料庫連線資訊都放在了threadlocal中 所以不同的
執行緒享用不同的連線資訊 所以不存在於乙個事務中
那麼我們則麼做到同步呢
在多次嘗試之後 終於寫出來了乙個demo 可以實現乙個執行緒報錯 全體回滾
相關**
@autowired
private platformtransactionmanager transactionmanager;
public
void
test()
throws interruptedexception
else
}catch
(exception e)})
.start()
;}trycatch
(exception e)
//主線程業務執行完畢 如果其他執行緒也執行完畢 且沒有報異常 正在阻塞狀態中 喚醒其他執行緒 提交所有的事務
//如果其他執行緒或者主線程報錯 則不會進入if 會觸發回滾if(
!rollbackflag.
get())
}
乙個多執行緒程式
這個程式會讓你深刻的體會時間片,執行緒的優先順序!建議多測試這個程式,一定要看下面的注。using system using system.collections.generic using system.text using system.threading namespace 乙個多執行緒程式 ...
乙個多執行緒程式
這個程式會讓你深刻的體會時間片,執行緒的優先順序!建議多測試這個程式,一定要看下面的注。using system using system.collections.generic using system.text using system.threading namespace 乙個多執行緒程式 ...
乙個多執行緒程式
這個程式會讓你深刻的體會時間片,執行緒的優先順序!建議多測試這個程式,一定要看下面的注。using system using system.collections.generic using system.text using system.threading namespace 乙個多執行緒程式 ...