• java 流不能复用 stream has already been operated upon or closed 内存分页


    场景

    //防止sql注入
        Stream<String> idList =accDocIDList.stream().map((x) ->
                    {
                    if (x.matches("[^a-zA-Z0-9-]"))
                    {
                        //throw new BalanceException("xxx-xxx", "hehe");
                        throw new RuntimeException("非法凭证ID");
                    }
                     return String.format("'%1$s'", x);
                });
    
            parameters = new IDbParameter[]
                    {
                            bqlExecuter.makeInParam("funcID", funcID),
                            bqlExecuter.makeInParam("userID", userID),
                            bqlExecuter.makeInParam("accOrgID", accOrgID),
                            bqlExecuter.makeInParam("AccLedgerID", ledger),
                            bqlExecuter.makeInParam("year", year),
                            bqlExecuter.makeInParam("flag", "1"),
                            bqlExecuter.makeInParam("accPeriodID", accPeriodID),
                    };
            //分页
            int pageSize = 900;
            int pageNum = 0;
            while (pageNum * pageSize < accDocIDList.size())
            {
                List<String> list = idList.skip(pageNum*pageSize).limit(pageSize).collect(Collectors.toList());  //此处会重新使用流 第二次循环会报错 stream has already been operated upon or closed 
                String idsql = bql + " and A.ID in (" + String.join(",", list) + ")";
                var ret = bqlExecuter.executeBqlStatement(idsql, refEntityIDs, parameters);
                pageNum++;
            }

    解决方法  使用Supplier

    //防止sql注入
            Supplier< Stream<String>> idList = ()->accDocIDList.stream().map((x) ->
                    {
                    if (x.matches("[^a-zA-Z0-9-]"))
                    {
                        //throw new BalanceException("xxx-xxx", "hehe");
                        throw new RuntimeException("非法凭证ID");
                    }
                     return String.format("'%1$s'", x);
                });
    
            parameters = new IDbParameter[]
                    {
                            bqlExecuter.makeInParam("funcID", funcID),
                            bqlExecuter.makeInParam("userID", userID),
                            bqlExecuter.makeInParam("accOrgID", accOrgID),
                            bqlExecuter.makeInParam("AccLedgerID", ledger),
                            bqlExecuter.makeInParam("year", year),
                            bqlExecuter.makeInParam("flag", "1"),
                            bqlExecuter.makeInParam("accPeriodID", accPeriodID),
                    };
            //分页
            int pageSize = 900;
            int pageNum = 0;
            while (pageNum * pageSize < accDocIDList.size())
            {
                List<String> list = idList.get().skip(pageNum*pageSize).limit(pageSize).collect(Collectors.toList());
                String idsql = bql + " and A.ID in (" + String.join(",", list) + ")";
                var ret = bqlExecuter.executeBqlStatement(idsql, refEntityIDs, parameters);
                pageNum++;
            }
  • 相关阅读:
    阿里云ECS 网站访问变卡
    MySql导入导出sql脚本(转)
    Mysql数据库备份的问题:mysqldump: Got error: 1049: Unknown database 'blog;' when selecting the database(转)
    Mysql in 优化(转)
    根据查询更新JTable显示(转)
    MyEclipse部署项目到Tomcat上,但是classes文件夹下没有编译项目(转)
    POI Workbook workbook = WorkbookFactory.create(fis); 报NoSuchMethodError
    07-常用内置对象
    06-流程控制
    05-数据类型转换
  • 原文地址:https://www.cnblogs.com/wolbo/p/12112203.html
Copyright © 2020-2023  润新知