title: MyBatis - Using Spring Transactions with Batch Operations
body: <p>I am migrating from iBatis 2.3 to MyBatis 3.2.8.  Everything is going well, but I do have a question when it comes to transactions and batch operations. Towards the bottom of <a href="https://mybatis.github.io/spring/sqlsession.html" rel="nofollow">https://mybatis.github.io/spring/sqlsession.html</a>, it says:</p>
  
  <blockquote>
    <p>The caveat to this form is that there cannot be an existing transaction running >with a different ExecutorType when this method is called. Either ensure that >calls to SqlSessionTemplates with different executor types run in a separate >transaction (e.g. with PROPAGATION_REQUIRES_NEW) or completely outside of a >transaction.</p>
  </blockquote>
  
  <p>I have a service call that is annotated with @Transactional that makes multiple Dao Calls:</p>
  
  <pre><code>@Override
  @Transactional
  public void saveMassUploadOrder(List&lt;IndividualOrder&gt; orders) {
     List&lt;IndividualOrderItem&gt; ioItems = new ArrayList&lt;IndividualOrderItem&gt;();
     for (IndividualOrder order : orders) {
        ioItems.addAll(order.getIoItems());
     }
     individualOrderDao.insertBatch(orders);
     individualOrderItemDao.insertBatch(ioItems);
  }
  </code></pre>
  
  <p>The DAOs would look like:</p>
  
  <pre><code>public class IndividualOrderDAOImpl  implements IndividualOrderDAO{
     private SqlSession sqlSession;
  
     public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
     }
  
     public void insertBatch(final List&lt;IndividualOrder&gt; orders) {
        for (IndividualOrder order: orders){
           sqlSession.insert("namespace.insert", order);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The config looks like:</p>
  
  <pre><code>&lt;bean id="batchSqlSession" class="org.mybatis.spring.SqlSessionTemplate"&gt;     
     &lt;constructor-arg index="0" ref="doeSqlSessionFactory" /&gt;
     &lt;constructor-arg index="1" value="BATCH" /&gt;
  &lt;/bean&gt;
  &lt;bean id="individualOrderDao" class="com.doe.mybatis.jdoe.dao.IndividualOrderDAOImpl"&gt;
      &lt;property name="sqlSession" ref="batchSqlSession" /&gt;
  &lt;/bean&gt;
  &lt;bean id="individualOrderItemDao" class="com.doe.mybatis.jdoe.dao.IndividualOrderItemDAOImpl"&gt;
     &lt;property name="sqlSession" ref="batchSqlSession" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>So - would the multiple DAO calls within this service call be OK since the service is opening a transaction and its not working in a batch mode and making calls to these DAOs? Obviously, I am wanting the call to this service to not  actually insert any orders or order items if there is a failure somewhere during the execution.  </p>
  
  <p>I haven't migrated these DAOs yet, and I can probably set up a catastrophic event during a test batch insert and check the data, I was just wanting to get some other opinions before I started to see if I have to set this up differently or not. </p>
  
  <p>Any help appreciated!</p>