一個(gè)曆史表中有大量的數(shù)據(jù),現(xiàn)在要通過(guò)分頁(yè)式查詢處理轉(zhuǎn)換數(shù)據(jù)。
現(xiàn)在將處理數(shù)據(jù)的邏輯放在線程池中處理,以加快處理流程。
可是總是出現(xiàn)事務(wù)方麵的異常
比如 : SQLNonTransientConnectionException
請(qǐng)問(wèn)該如何解決上述異常
,或者有什么好的多線程分頁(yè)查詢處理方案
?
原來(lái)問(wèn)題描述不太清楚,現(xiàn)在添加以下代碼(手敲,如果有錯(cuò),請(qǐng)多包含)
分頁(yè)式查詢邏輯:
int pageSize = 100;
int currentPageLength = 0;
int pageIndex = 0;
ExecutorService exe = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
do {
int offset = pageIndex * pageSize;
List<TradeInfo> tradeInfos = tradeInfoService.findTradeInfoBysPage(queryParams,offset,pageSize);
if (null != tradeInfos && tradeInfos.size() > 0) {
currentPageLength = tradeInfos.size();
TradeInfoProcesserTask task = new TradeInfoProcesserTask(tradeInfos );
exe.execute(task);
pageIndex++;
}else{
System.out.println("Page Query TradeInfo Got NOTHING! Break query loop!");
break;
}
} while (currentPageLength == pageSize);
exe.shutdown();
while(true) {
if(exe.isTerminated()){
doOtherThings();
System.out.println("分頁(yè)式多線程處理數(shù)據(jù)完畢!");
break;
}
}
數(shù)據(jù)處理邏輯:
public class TradeInfoProcesserTask implements Runnable{
private volatile List<TradeInfo> tradeInfos;
public TradeInfoProcesserTask (List<TradeInfo> _tradeInfos){
tradeInfos = _tradeInfos;
}
@Override
public void run() {
processTradeInfos();
}
private void processTradeInfos(){
//do something with tradeInfos .....
}
}
歡迎選擇我的課程,讓我們一起見(jiàn)證您的進(jìn)步~~
邏輯先不說(shuō)。
現(xiàn)在沒(méi)有判斷多執(zhí)行緒是否全部執(zhí)行完,while循環(huán)完就shutdown。 。 。
將CountDownLatch透過(guò)建構(gòu)器傳入執(zhí)行緒
ExecutorService exe = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
CountDownLatch latch = new CountDownLatch(?); //?代表開(kāi)啟全部線程的數(shù)量
do {
int offset = pageIndex * pageSize;
List<TradeInfo> tradeInfos = tradeInfoService.findTradeInfoBysPage(queryParams,offset,pageSize);
if (null != tradeInfos && tradeInfos.size() > 0) {
currentPageLength = tradeInfos.size();
TradeInfoProcesserTask task = new TradeInfoProcesserTask(tradeInfos, latch);
exe.execute(task);
pageIndex++;
}else{
System.out.println("Page Query TradeInfo Got NOTHING! Break query loop!");
break;
}
} while (currentPageLength == pageSize);
latch.await(); //多線程全部執(zhí)行完
exe.shutdown();
doOtherThings();
System.out.println("分頁(yè)式多線程處理數(shù)據(jù)完畢!");
public class TradeInfoProcesserTask implements Runnable{
private volatile List<TradeInfo> tradeInfos;
private CountDownLatch latch;
public TradeInfoProcesserTask (List<TradeInfo> _tradeInfos, CountDownLatch latch){
tradeInfos = _tradeInfos;
this.latch = latch;
}
@Override
public void run() {
processTradeInfos();
latch.countDown();
}
private void processTradeInfos(){
//do something with tradeInfos .....
}
}
分頁(yè)查詢是不並發(fā)(DAO)的,資料處理是並發(fā)(Service),所以你的事務(wù)級(jí)等級(jí)是設(shè)定在哪個(gè)環(huán)節(jié)呢?