iBATIS3 トランザクション(1)

ここに書いたもより、こちらを使うべき
http://blog.zaq.ne.jp/oboe2uran/article/458/

http://blog.zaq.ne.jp/oboe2uran/article/459/

http://blog.zaq.ne.jp/oboe2uran/article/460/
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

-------------------------------------


iBATIS で不充分だったトランザクション管理を行いたくて
Google プロジェクト ibaguice  を覗いてみた。
まだ開発中でSVNを覗くと予想どおり
org.aopalliance.intercept.MethodInterceptor を使用していくようだ。
ibaguice は、SQLMapというよりDAOベースで、iBATIS に良いところが消えてく気がした。

MethodInterceptor で簡易にトランザクション管理を書けるのではと思い書いてみる。
(計2回に分けて投稿します)

まず最初に、アノテーションインターセプト

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * トランザクション処理を約束する.
 * 対象となるメソッドと private フィールドで
 * 宣言して使用するSqlSession もしくは、マッパーに付与する
 */

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD})
public @interface Transaction{
}

-------------------------------------------------------------------
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import com.google.inject.Inject;
/**
 * トランザクションとしてインターセプト処理.
 */

public final class TransactionExecutor implements MethodInterceptor{
   private SqlSessionFactory sqlSessionFactory;

   @Inject
   public TransactionExecutor(SqlSessionFactory sqlSessionFactory){
      this.sqlSessionFactory = sqlSessionFactory;
   }

   @Override
   public Object invoke(MethodInvocation m) throws Throwable
{
      Object rtn = null;
      SqlSession session = null;
      try{
      session = this.sqlSessionFactory.openSession();


      Field setField = this.getAnotatedField(Transaction.class
                                            ,m.getMethod().getDeclaringClass());
      setField.setAccessible(true);
      if (setField.getType().equals(SqlSession.class)){
         setField.set(m.getThis(),session);
      }else{
         setField.set(m.getThis(),session.getMapper(setField.getType()));
      }

      rtn = m.proceed();

      session.commit();

      }catch(Exception e){
         if (session != null){
            session.rollback();
         }

         throw e;
      }finally{
         if (session != null) session.close();
      }
      return rtn;
   }
   private Field getAnotatedField(Class<? extends Annotation> a,Class<?> cls){
      Field rtn=null;
      Field fls = cls.getDeclaredFields();
      for(int i=0;i < fls.length;i++){
         Annotation
 as = fls[i].getAnnotations();
         if (as != null){
            for(int k=0;k < as.length;k++){
               if (as[k].annotationType().equals(a)){
                  rtn = fls[i];
                  i = fls.length;
                  break;
               }
            }
         }
      }
      return rtn;
   }
}

-------------------------------------------------------------------
続きは次回、