Open btbox opened 2 years ago
很奇怪的是,我之前是2000为一次batch插入是发生这个异常,换成 1W一次batchInsert没有问题
<insert id="batchInsertVertex" >
INSERT VERTEX ${tagName}
(
@for ( param in params ) {
${param}${paramLP.last?"":"," }
@}
)
VALUES
${dataStr};
</insert>
感觉有可能是因为2000条一页的时候开的线程数太多了?
但是我用的是线程池,6 core, 12 线程 队列 是 9999
如上所述,450页的话,就会循环450次 @Async里面的线程方法去batchInsert
@Bean(name = "graphAsyncImportExecutor")
public Executor graphAsyncImportExecutor() {
//在这里修改
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//配置核心线程数
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
//配置最大线程数
executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
//配置队列大小
executor.setQueueCapacity(queueCapacity);
//配置线程池中的线程的名称前缀
executor.setThreadNamePrefix(namePrefix);
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler( new ThreadPoolExecutor.CallerRunsPolicy());
//执行初始化
executor.initialize();
return executor;
}
既然线程有不大的上限,所开线程一致的话,按道理2000一次所占用的内存会更小才对,是有些反直觉。
不好意思,刚刚看错了... 还以为是OutOfMemoryException。
报错有 Cause by 信息吗
Unexpected exception occurred invoking async method: public void com.box.modules.work.graph_import.async.GraphImportAsync.asyncBatchInsertGraph(java.lang.String,java.lang.String,java.lang.String,java.util.Map,java.util.List)
java.lang.StackOverflowError: null
at com.box.modules.work.graph_import.service.impl.GraphBaseApiImpl.batchInsertVertex(GraphBaseApiImpl.java:126)
at com.box.modules.work.graph_import.async.GraphImportAsync.asyncBatchInsertGraph(GraphImportAsync.java:45)
at com.box.modules.work.graph_import.async.GraphImportAsync$$FastClassBySpringCGLIB$$5ed53427.invoke(
xml 看上去没有问题,如果dataStr每条写入值的栏位跟params一致,应该不会有太大问题。
批量插入导致 java.lang.StackOverflowError: null
我的逻辑是从数据库中分页查询出数据,然后分批次 多线程批量插入,例如:九十万数据,分 2000条数据,总共450页。
错误栈如下