2

DB2 テーブルからデータを抽出し、返された各行に対して何らかの処理を実行し、フラット ファイルに出力する必要があります。私は iBatis を使用していますが、queryForListを使用するとメモリ不足エラーが発生し始めたことがわかりました。10 万行以上のデータが増加していることがわかります。

代わりにqueryWithRowHandlerを使用することを検討しましたが、iBatis RowHandlerインターフェイスはhandleRow関数から例外をスローしないため、エラーが発生した場合、適切に報告できず、残りのデータの繰り返しを停止できません。RuntimeException をスローできるように見えますが、それは適切な方法とは思えません。

エラーがデータ操作、ファイルアクセスなどで発生したかどうかを示す意味のある例外をスローしながら、処理を停止できるようにしたいと考えています。

このアプローチの経験がある人、またはiBatisを使用した代替ソリューションを持っている人はいますか? JDBC を使用するだけで、iBatis を使用せずにこれを行うことができることはわかっていますが、iBatis はアプリ内の他のすべての DB アクセスに使用されるため、可能であればこのアーキテクチャを利用したいと考えています。

4

1 に答える 1

3

1)署名の例外をチェックして独自のRowHandlerインターフェースを作成します。

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

2) SqlMapDaoTemplateから継承(またはさらに良いのはデリゲート)して、署名に同じ例外がある独自のハンドラーを管理する新しいメソッドを追加します。

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) {
    ...
于 2009-08-28T14:39:23.967 に答える