1 回答

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
我發(fā)現(xiàn)了如何在 ForkJoinPool 的任務(wù)中設(shè)置事務(wù)。我只需像下面那樣使用 TransactionSynchronizationManager。
@Transactional(readOnly = true)
public void streamTest() {
ForkJoinPool customThreadPool = new ForkJoinPool(20);
try {
customThreadPool.submit(() -> {
TransactionSynchronizationManager.setActualTransactionActive(true);
TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);
TransactionSynchronizationManager.initSynchronization();
priceRepository.streamAll()
.parallel()
.map(p -> this.transform(p))
.forEach(System.out::println);
}).get();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
添加回答
舉報(bào)