zoukankan      html  css  js  c++  java
  • 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++;
            }
  • 相关阅读:
    Cuckoo for Hashing_双哈希表
    nyoj113_字符串替换
    nyoj366_D的小L_字典序_全排列
    二叉树的前序 中序 后序 遍历(递归/非递归)
    Java 学习路线
    leetcode 04 Median of Two Sorted Arrays
    ThreadLocal 的机制与内存泄漏
    try finally 执行顺序问题
    Java中的类加载器
    快速理解Java中的七种单例模式
  • 原文地址:https://www.cnblogs.com/wolbo/p/12112203.html
Copyright © 2011-2022 走看看