【问题标题】:Java - retrieving large amounts of data from a DB using iBatisJava - 使用 iBatis 从数据库中检索大量数据
【发布时间】:2010-11-23 13:57:34
【问题描述】:

我需要从 DB2 表中提取数据,对每个返回的行运行一些处理并输出到一个平面文件。我正在使用 iBatis,但发现使用 queryForList 我开始出现内存不足错误,我将看到 100k+ 行数据增加。

我看过使用 queryWithRowHandler 代替,但 iBatis RowHandler 接口不会引发异常来自它的 handleRow 函数,所以如果它出现错误,我无法正确报告它并停止迭代其余数据。看起来我可以抛出一个 RuntimeException ,但这并没有让我觉得这是一种简洁的做事方式。

我希望能够在抛出有意义的异常时停止处理,指示错误是否发生在数据操作、文件访问或其他方面。

有没有人使用过这种方法或有使用 iBatis 的替代解决方案。我知道我可以在不使用 iBatis 的情况下执行此操作,只使用 JDBC,但由于 iBatis 用于我的应用程序中的所有其他数据库访问,因此我希望尽可能利用此架构。

【问题讨论】:

    标签: java sql jdbc db2 ibatis


    【解决方案1】:

    1) 创建您自己的 RowHandler 接口,并在签名中检查异常:

    public interface MySpecialRowHandler {
        public void handleRow(Object row) 
            throws DataException, FileException, WhateverException;
    }
    

    2) 从 SqlMapDaoTemplate 继承(甚至更好,delegate)以添加一个新方法,该方法将使用签名中相同的异常来管理您自己的处理程序:

    public class MySpecialTemplate extends SqlMapDaoTemplate {
        ...
        public void queryWithRowHandler(String id, 
            final MySpecialRowHandler myRowHandler
        ) throws DataException, FileException, WhateverException {
            // "holder" will hold the exception thrown by your special rowHandler
            // both "holder" and "myRowHandler" need to be declared as "final"
            final Set<Exception> holder = new HashSet<Exception>();
            this.queryWithRowHandler(id,new RowHandler() {
                public void handleRow(Object row) {
                    try {
                        // your own row handler is executed in IBatis row handler
                        myRowHandler.handleRow(row);
                    } catch (Exception e) {
                        holder.add(e);
                    }
                }
            });
            // if an exception was thrown, rethrow it.
            if (!holder.isEmpty()) {
                Exception e = holder.iterator().next();
                if (e instanceof DataException)     throw (DataException)e;
                if (e instanceof FileException)     throw (FileException)e;
                if (e instanceof WhateverException) throw (WhateverException)e;
                // You'll need this, in case none of the above works
                throw (RuntimeException)e;
            }
        }
    }                    
    

    3) 您的业务代码将如下所示:

    // create your rowHandler
    public class Db2RowHandler implements MySpecialRowHandler {
        void handleRow(Object row) throws DataException, FileException, WhateverException {
            // what you would have done in ibatis RowHandler, with your own exceptions
        }
    }
    // use it.
    MySpecialTemplate template = new MySpecialTemplate(daoManager);
    try {
        template.queryWithRowHandler("selectAllDb2", new Db2RowHandler());
    } catch (DataException e) {
        // ...
    } catch (FileException e) {
        ...
    

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 2022-08-03
      • 2013-10-31
      • 1970-01-01
      • 2011-11-24
      • 2012-03-09
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      相关资源
      最近更新 更多