SqlSessionFactory

iBATIS3 Transaction の使い方が、まだ違和感がある。

SQLバッチ実行の場合、
SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsReader("Configuration.xml"));
SqlSession sqlsession = factory.openSession(ExecutorType.BATCH,false);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Transaction tx = transactionFactory.newTransaction(sqlsession.getConnection(),false);

で作っても、tx.commit() では、ダメだった。
なぜか、sqlsession.commit() ならうまくいく。
SQLバッチ実行でない
SqlSession sqlsession = factory.openSession();
で、tx.commit() ならばもちろんOK


そこで、SqlSession 生成インジェクトに、Bindingアノテーションで区別することを
考えた。

---------------------------------------------------------
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.google.inject.BindingAnnotation;

/**
 * SQL をBATCH実行する時に、
 * @Inject @BatchSQL private SqlSession s;
 * のようにインジェクトされるように使用する。
 */

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

   @Provides
   protected SqlSession providedSqlSession() throws IOException{
      SqlSessionFactory factory = new SqlSessionFactoryBuilder()
   .build(Resources.getResourceAsReader("Configuration.xml"));
      return factory.openSession();
   }
   @Provides @BatchSQL
   protected SqlSession providedBatchSqlSession() throws IOException{
      SqlSessionFactory factory = new SqlSessionFactoryBuilder()
   .build(Resources.getResourceAsReader("Configuration.xml"));
      return factory.openSession(ExecutorType.BATCH,false);
   }