一个历史表中有大量的数据,现在要通过分页式查询处理转换数据。
现在将处理数据的逻辑放在线程池中处理,以加快处理流程。
可是总是出现事务方面的异常
比如 : SQLNonTransientConnectionException
请问该如何解决上述异常
,或者有什么好的多线程分页查询处理方案
?
原来问题描述不太清楚,现在添加以下代码(手敲,如果有错,请多包含)
分页式查询逻辑:
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("分页式多线程处理数据完毕!");
break;
}
}
数据处理逻辑:
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 .....
}
}
Let’s not talk about logic.
Now there is no judgment whether all multi-threads have been executed. The while loop will be shut down. . .
Pass CountDownLatch into the thread through the constructor
Paging query is not concurrent (DAO), data processing is concurrent (Service), so where is your transaction level set?