postgresql: IOException: Tried to send an out-of-range integer as a 2-byte value: 35834

1.现状:调用批量插入数据库方法,报错如下:

Dec 13, 2023 @ 10:41:30.706    org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
Dec 13, 2023 @ 10:41:30.706        at org.postgresql.core.PGStream.sendInteger2(PGStream.java:359)
Dec 13, 2023 @ 10:41:30.706    Caused by: java.io.IOException: Tried to send an out-of-range integer as a 2-byte value: 35834
Dec 13, 2023 @ 10:41:30.706        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
Dec 13, 2023 @ 10:41:30.706        at com.sun.proxy.$Proxy196.insertNoIdBatch(Unknown Source)
Dec 13, 2023 @ 10:41:30.706        at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:50)
Dec 13, 2023 @ 10:41:30.706        at sun.reflect.GeneratedMethodAccessor387.invoke(Unknown Source)
Dec 13, 2023 @ 10:41:30.706        at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272)
Dec 13, 2023 @ 10:41:30.706        at com.sun.proxy.$Proxy283.execute(Unknown Source)
Dec 13, 2023 @ 10:41:30.706        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
Dec 13, 2023 @ 10:41:30.706    ### Error updating database.  Cause: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.

postgresql: IOException: Tried to send an out-of-range integer as a 2-byte value: 35834_第1张图片

mapper.xml 使用foreach方法:


        insert into ics_check_report
        (geom, batch_id,check_name, check_type, task_id, data_version,
        layer_name, feature_id, chk_id,misrep_id, error_code, error_desc,
        feature_wkt, create_time, location,trace_time,error_repair,feat_attrs,
        err_subtype,ref_layer2id,tile_id,field_name,mis_info,imp_level,step,feedback,
        chk_version, suggest, adcity_code, geom_shape, repair_status, scene_type, str_geom,sub_task_id, diff_flag,
        mis_flag)
        values
        
            (
            ST_Transform(st_geomfromtext(#{icsCheckReport.featureWkt,jdbcType=VARCHAR},
            #{icsCheckReport.srid,jdbcType=INTEGER}), 4326),
            #{icsCheckReport.batchId,jdbcType=INTEGER},
            #{icsCheckReport.checkName,jdbcType=VARCHAR},
            #{icsCheckReport.checkType,jdbcType=VARCHAR},
            #{icsCheckReport.taskId,jdbcType=VARCHAR},
            #{icsCheckReport.dataVersion,jdbcType=VARCHAR},
            #{icsCheckReport.layerName,jdbcType=VARCHAR},
            #{icsCheckReport.featureId,jdbcType=VARCHAR},
            #{icsCheckReport.chkId,jdbcType=VARCHAR},
            #{icsCheckReport.misrepId,jdbcType=INTEGER},
            #{icsCheckReport.errorCode,jdbcType=INTEGER},
            #{icsCheckReport.errorDesc,jdbcType=VARCHAR},
            #{icsCheckReport.featureWkt,jdbcType=VARCHAR},
            
            #{icsCheckReport.createTime},
            #{icsCheckReport.location,jdbcType=OTHER},
            #{icsCheckReport.traceTime},
            #{icsCheckReport.errorRepair,jdbcType=OTHER},
            #{icsCheckReport.featAttrs,jdbcType=OTHER},
            #{icsCheckReport.errSubtype,jdbcType=VARCHAR},
            #{icsCheckReport.refLayer2id,jdbcType=OTHER},
            #{icsCheckReport.tileId,jdbcType=VARCHAR},
            #{icsCheckReport.fieldName,jdbcType=VARCHAR},
            #{icsCheckReport.misInfo,jdbcType=VARCHAR},
            #{icsCheckReport.impLevel,jdbcType=VARCHAR},
            #{icsCheckReport.step,jdbcType=INTEGER},
            #{icsCheckReport.feedback,jdbcType=INTEGER},
            #{icsCheckReport.chkVersion,jdbcType=VARCHAR},
            #{icsCheckReport.suggest,jdbcType=VARCHAR},
            #{icsCheckReport.adcityCode,jdbcType=VARCHAR},
            st_astext(ST_Transform(st_geomfromtext(#{icsCheckReport.geomShape,jdbcType=VARCHAR},
            #{icsCheckReport.srid,jdbcType=INTEGER}), 4326)),
            #{icsCheckReport.repairStatus,jdbcType=INTEGER},
            #{icsCheckReport.sceneType,jdbcType=INTEGER},
            #{icsCheckReport.strGeom,jdbcType=VARCHAR},
            #{icsCheckReport.subTaskId,jdbcType=VARCHAR},
            #{icsCheckReport.diffFlag,jdbcType=VARCHAR},
            #{icsCheckReport.misFlag,jdbcType=VARCHAR}
            )
        
    

然后调用 int insertNoIdBatch(List recordList) 中的recordList 一次性给1000条。

2.原因:

        mybatis-plus向postgresql插入批量数据,数据库的JDBC驱动对参数数量是有上限限制的,最大值为32767。然而使用上面的insertNoIdBatch方法中的字段有40个*1000=40000个参数(每条数据循环拼接参数累计),超过了32767。

3.解决办法:

控制入库list数据数量:

/**
 * 递归插入
 * 
 */ 
public void insertForeach(List all, long start, long limit) {
        //截取 从start开始截取 截取limit条
        List collect = all.stream().skip(start).limit(limit).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return;
        }
        //批量插入数据的方法
        int count = icsCheckReportMapper.insertNoIdBatch(collect);
        // oneMapIcsCheckReportService.saveBatch(collect);
        if (count > 0) {
            log.info("入库数量:{} insertList:{}", count, collect.size());
        }
        //递归 每次插入limit条数据
        insertForeach(all, start + limit, limit);
    }


/*
* 调用批量插入方法
* insertList.size >=1000
*/
public void insertReport(List insertList){
     int insertSize = 500;
     // 插入报表库
     insertForeach(insertList, 0, insertSize);
}

500条数据插入提交一次,那总字段数:500*40=20000个,小于32767,保存成功。

你可能感兴趣的:(Java,postgresql,mybatis)