传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。此 RPC 请求中提供了过多的参数。最多应为 2100。

Error updating database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。此 RPC 请求中提供了过多的参数。最多应为 2100。

这是因为集合数据量过大,需要对集合进行拆分操作,就是将大集合折分成几个小集合,再进行分批操作,如:1,2,3,4,5,6,7 按三个一组拆分成 \[1,2,3、4,5,6、7]

java 复制代码
    // 批量增加受理样品
    @Override
    public void addBatch(List<ApplySample> applySampleList) {
        // 如果样品过多,SQL语句的参数大于2100,会报错【此 RPC 请求中提供了过多的参数。最多应为 2100】
        // applySampleMapper.insertBatch(applySampleList);
        /*
        这是插入一条样品信息的SQL语句,用到参数29个,2100 / 29 = 72.4,所以单次最多插入72条样品信息,最好插入50条样品信息
        <!--批量增加受理样品-->
        <insert id="insertBatch">
        <foreach collection="list" item="item">
            if not exists(select as_ID from ApplySampleInfoC where as_OuterApplyId = #{item.outerApplyId} and as_ApplyID = #{item.applyId} and as_SampleNo = #{item.sampleNo})
            insert into ApplySampleInfoC(
            as_OuterApplyID, as_ApplyID, as_SampleNo,
            as_SampleName, as_SampleType, as_YCDD, as_SCDW,
            as_SCDWDZ, as_BatchNo, as_TradeMark, as_Packing,
            as_Property, as_Specs, as_CheckCondition, as_Amount, as_Unit,
            as_Gerder, as_Age, as_SJRQ, as_Vocation,
            as_nationality, as_Ethnics, as_Marriage, as_Education,
            as_IdentityCard, as_Telephone, as_Address,
            as_IdentityAddress, as_TakeMedicineSolutions)
            values(#{item.outerApplyId}, #{item.applyId}, #{item.sampleNo},
            #{item.sampleName}, #{item.sampleType}, #{item.location}, #{item.scdwName},
            #{item.scdwAddress}, #{item.batchNo}, #{item.trademark}, #{item.packing},
            #{item.property}, #{item.specs}, #{item.condition}, #{item.amount}, #{item.unit},
            #{item.gender}, #{item.age}, #{item.peopleGroup}, #{item.vocation},
            #{item.nationality}, #{item.ethnics}, #{item.marriage}, #{item.education},
            #{item.identityNumber}, #{item.telephone}, #{item.residenceAddress},
            #{item.identityAddress}, #{item.previousMedicalHistory})
        </foreach>
        </insert>
         */
        // 批处理,拆分数据,满50条,执行批量插入
        if (applySampleList != null && applySampleList.size() > 50) {
            List<ApplySample> applySampleNewList = new ArrayList<>();
            // 遍历提取数据
            for (int i = 0; i < applySampleList.size(); i++) {
                applySampleNewList.add(applySampleList.get(i));
                // 满50条数据,提交一次
                if ((i + 1) % 50 == 0) {
                    applySampleMapper.insertBatch(applySampleNewList);
                    applySampleNewList.clear();
                }
            }
            // 最后一次提交
            if (!applySampleNewList.isEmpty()) {
                applySampleMapper.insertBatch(applySampleNewList);
                applySampleNewList.clear();
            }
        } else{ // 不够50条数据,不需要分批,直接插入
            // 列表有数据才进行插入,否则不执行任何操作。这样写代码才健壮
            if (applySampleList != null && !applySampleList.isEmpty()) {
                applySampleMapper.insertBatch(applySampleList);
            }
        }
    }

对上面代码进行优化:

java 复制代码
    // 批量增加受理样品
    @Override
    public void addBatch(List<ApplySample> applySampleList) {
        // 如果样品过多,SQL语句的参数大于2100,会报错【此 RPC 请求中提供了过多的参数。最多应为 2100】
        // applySampleMapper.insertBatch(applySampleList);
        /*
        这是插入一条样品信息的SQL语句,用到参数29个,2100 / 29 = 72.4,所以单次最多插入72条样品信息,最好插入50条样品信息
        <!--批量增加受理样品-->
        <insert id="insertBatch">
        <foreach collection="list" item="item">
            if not exists(select as_ID from ApplySampleInfoC where as_OuterApplyId = #{item.outerApplyId} and as_ApplyID = #{item.applyId} and as_SampleNo = #{item.sampleNo})
            insert into ApplySampleInfoC(
            as_OuterApplyID, as_ApplyID, as_SampleNo,
            as_SampleName, as_SampleType, as_YCDD, as_SCDW,
            as_SCDWDZ, as_BatchNo, as_TradeMark, as_Packing,
            as_Property, as_Specs, as_CheckCondition, as_Amount, as_Unit,
            as_Gerder, as_Age, as_SJRQ, as_Vocation,
            as_nationality, as_Ethnics, as_Marriage, as_Education,
            as_IdentityCard, as_Telephone, as_Address,
            as_IdentityAddress, as_TakeMedicineSolutions)
            values(#{item.outerApplyId}, #{item.applyId}, #{item.sampleNo},
            #{item.sampleName}, #{item.sampleType}, #{item.location}, #{item.scdwName},
            #{item.scdwAddress}, #{item.batchNo}, #{item.trademark}, #{item.packing},
            #{item.property}, #{item.specs}, #{item.condition}, #{item.amount}, #{item.unit},
            #{item.gender}, #{item.age}, #{item.peopleGroup}, #{item.vocation},
            #{item.nationality}, #{item.ethnics}, #{item.marriage}, #{item.education},
            #{item.identityNumber}, #{item.telephone}, #{item.residenceAddress},
            #{item.identityAddress}, #{item.previousMedicalHistory})
        </foreach>
        </insert>
         */
        // 集合不为空
        if (!applySampleList.isEmpty()) {
            // 批处理,拆分数据,满50条,执行批量插入
            List<List<ApplySample>> lists = splitList(applySampleList, 50);
            for (List<ApplySample> list: lists) {
                applySampleMapper.insertBatch(list);
            }
        }

        
        // 集合分割,将一个大集合分割成多个小集合,数据示例:[1,2,3,4,5,6,7] 按3个元素一组分割成 [[1,2,3],[4,5,6],[7]]
        public List<List<ApplySample>> splitList(List<ApplySample> list, int chunkSize) {
            List<List<ApplySample>> result = new ArrayList<>();

            for (int i = 0; i < list.size(); i += chunkSize) {
                int end = Math.min(i + chunkSize, list.size());
                result.add(list.subList(i, end));
            }

            return result;
        }
    }

对上面代码再一步优化:

1、提取集合拆分的逻辑,做成集合拆分通用工具

java 复制代码
package com.weiyu.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PublicUtils {
    /**
     * 集合分割,将一个大集合分割成多个小集合,数据示例:[1,2,3,4,5,6,7] 按3个元素一组分割成 [[1,2,3],[4,5,6],[7]]
     * @param list 集合
     * @param chunkSize 分割大小
     * @return 返回分割集合
     */
    public static <E> Collection<List<E>> splitList(List<E> list, int chunkSize) {
        List<List<E>> result = new ArrayList<>();

        for (int i = 0; i < list.size(); i += chunkSize) {
            int end = Math.min(i + chunkSize, list.size());
            result.add(list.subList(i, end));
        }

        return result;
    }
}

2、使用集合拆分通用工具

java 复制代码
    // 批量增加受理样品
    @Override
    public void addBatch(List<ApplySample> applySampleList) {
        // 如果样品过多,SQL语句的参数大于2100,会报错【此 RPC 请求中提供了过多的参数。最多应为 2100】
        // applySampleMapper.insertBatch(applySampleList);
        /*
        这是插入一条样品信息的SQL语句,用到参数29个,2100 / 29 = 72.4,所以单次最多插入72条样品信息,最好插入50条样品信息
        <!--批量增加受理样品-->
        <insert id="insertBatch">
        <foreach collection="list" item="item">
            if not exists(select as_ID from ApplySampleInfoC where as_OuterApplyId = #{item.outerApplyId} and as_ApplyID = #{item.applyId} and as_SampleNo = #{item.sampleNo})
            insert into ApplySampleInfoC(
            as_OuterApplyID, as_ApplyID, as_SampleNo,
            as_SampleName, as_SampleType, as_YCDD, as_SCDW,
            as_SCDWDZ, as_BatchNo, as_TradeMark, as_Packing,
            as_Property, as_Specs, as_CheckCondition, as_Amount, as_Unit,
            as_Gerder, as_Age, as_SJRQ, as_Vocation,
            as_nationality, as_Ethnics, as_Marriage, as_Education,
            as_IdentityCard, as_Telephone, as_Address,
            as_IdentityAddress, as_TakeMedicineSolutions)
            values(#{item.outerApplyId}, #{item.applyId}, #{item.sampleNo},
            #{item.sampleName}, #{item.sampleType}, #{item.location}, #{item.scdwName},
            #{item.scdwAddress}, #{item.batchNo}, #{item.trademark}, #{item.packing},
            #{item.property}, #{item.specs}, #{item.condition}, #{item.amount}, #{item.unit},
            #{item.gender}, #{item.age}, #{item.peopleGroup}, #{item.vocation},
            #{item.nationality}, #{item.ethnics}, #{item.marriage}, #{item.education},
            #{item.identityNumber}, #{item.telephone}, #{item.residenceAddress},
            #{item.identityAddress}, #{item.previousMedicalHistory})
        </foreach>
        </insert>
         */
        // 集合不为空
        if (!applySampleList.isEmpty()) {
            // 批处理,拆分数据,满50条,执行批量插入
            List<List<ApplySample>> lists = (List<List<ApplySample>>)PublicUtils.splitList(applySampleList, 50);
            for (List<ApplySample> list: lists) {
                applySampleMapper.insertBatch(list);
            }
        }
    }
相关推荐
nice_lcj5203 小时前
【从零理解计算机网络 · 13】DNS 解析全流程:域名怎么变成 IP,慢在哪
网络·计算机网络·面试·dns·域名解析·cname·ttl
LabVIEW开发8 小时前
Ethernet/IP 会话无效 -251723770
服务器·网络协议·tcp/ip
张张123y11 小时前
个人助手型 Agent 的六个工程细节:Hermes 与 OpenClaw 的审批、委派、上下文与调度
网络·人工智能·开源·后端开发·ai agent
nice_lcj52012 小时前
【从零理解计算机网络 · 15】完整串联:一次请求的全旅程,以及那道经典面试题
网络·计算机网络·http·面试·tcp·总结·网络排查
光电笑映1 天前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(下)
linux·网络·网络协议·tcp
如夏倾城1 天前
入侵检测系统(IDS)原理、部署模式与 IPS/防火墙对比
大数据·网络·人工智能·网络安全·云原生·架构·kubernetes
LabVIEW开发1 天前
LabVIEW 用 25 ns 精度捕捉赛车发动机共振
网络·labview·labview知识·labview功能·labview程序
赤生云电竞1 天前
云游戏边缘节点部署实践:机房选址与网络拓扑选型
网络·游戏·php
ITxiaobing20231 天前
中国广告技术公司海外反欺诈中的 IP 应用:从“库查询”到“多层信号”
网络·tcp/ip·web安全
安易算力1 天前
智算中心网络架构选型:RDMA/IB/以太网深度对比与落地实践
网络·架构