2009-08-27 61 views
2

DB2テーブルからデータを抽出し、返された行ごとに処理を行い、フラットファイルに出力する必要があります。私はiBatisを使用していますが、使用していることがわかりましたqueryForList私はメモリエラーから脱却し始めました。Java - iBatisを使用してDBから大量のデータを取得する

私はそれは私がきちんとできないエラーが発生しますので、もしインタフェースはそのhandleRow関数から例外をスローしない代わりに、queryWithRowHandlerを使用しますがiBatisのRowHandlerを見てきましたそれを報告し、残りのデータの反復処理を停止します。私はRuntimeExceptionをスローすることができるように見えるが、それは物事を行うきちんとした方法として私を攻撃しない。

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

誰でもこのアプローチを経験しているか、iBatisを使用している代替ソリューションがありますか。私はiBatisなしでこれを行うことができることを知っているだけで、JDBCを使用していますが、iBatisは私のアプリで他のすべてのDBアクセスに使用されていますので、可能であればこのアーキテクチャを利用したいと思います。

答えて

3

1)署名でチェック例外を使用して独自のRowHandlerインタフェースを作成します。SqlMapDaoTemplateからさらに良い

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

2)継承(または、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) { 
    ... 
関連する問題