title: Illegal attempt to enlist multiple 1PC XAResources
body: <p>We have Multiple Transaction Managers (<code>WebsphereUOWTransactionManager</code> and <code>DatasourceTransactionManager</code>) and use Mybatis and <code>NamedParameterJdbcTemplate</code> (legacy code)</p>
  
  <p>The code used to work and the changes made are to move <code>@Transactional</code> to the Service Level and remove <code>sqlsession.commit</code> from the mybatis code.</p>
  
  <p>But now there is an issue where the legacy code (<code>JdbcTemplate</code>) is executed. </p>
  
  <p><strong>The error is</strong></p>
  
  <pre><code>20170427 19:22:02.045 [SimpleThreadPoolWorker-2] DEBUG o.s.b.c.r.d.JdbcStepExecutionDao # Truncating long message before update of StepExecution, original message is: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [
                  UPDATE JOB_RUN
                  SET SYS_RUN_STUS_CD = ?,
                  UPDT_USER_ID = 'SYSTEM',
                  REC_UPDT_TS = CURRENT_TIMESTAMP
                  WHERE  JOB_RUN_ID = ?
              ]; SQL state [null]; error code [0]; enlist: caught Exception; nested exception is java.sql.SQLException: enlist: caught Exception
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
      at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
      at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909)
      at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:933)
      at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:313)
      at com.abc.common.dao.jdbc.JobRunJdbcDao.updateJobStatus(JobRunJdbcDao.java:199)
      at com.abc.common.dao.jdbc.JobRunJdbcDao.updateJobStatus(JobRunJdbcDao.java:193)
      at com.abc.bpe.core.InitializingTasklet.execute(InitializingTasklet.java:59)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport$1.doInTransaction(TransactionAspectSupport.java:304)
      at org.springframework.transaction.jta.WebSphereUowTransactionManager$UOWActionAdapter.run(WebSphereUowTransactionManager.java:347)
      at com.ibm.ws.uow.embeddable.EmbeddableUOWManagerImpl.runUnderCurrentUOW(EmbeddableUOWManagerImpl.java:954)
      at com.ibm.ws.uow.embeddable.EmbeddableUOWManagerImpl.runUnderUOW(EmbeddableUOWManagerImpl.java:370)
      at org.springframework.transaction.jta.WebSphereUowTransactionManager.execute(WebSphereUowTransactionManager.java:290)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:298)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy118.execute(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
      at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy106.execute(Unknown Source)
      at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
      at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
      at org.springframework.transaction.jta.WebSphereUowTransactionManager$UOWActionAdapter.run(WebSphereUowTransactionManager.java:347)
      at com.ibm.ws.uow.embeddable.EmbeddableUOWManagerImpl.runUnderNewUOW(EmbeddableUOWManagerImpl.java:775)
      at com.ibm.ws.uow.embeddable.EmbeddableUOWManagerImpl.runUnderUOW(EmbeddableUOWManagerImpl.java:368)
      at org.springframework.transaction.jta.WebSphereUowTransactionManager.execute(WebSphereUowTransactionManager.java:290)
      at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:127)
      at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
      at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
      at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
      at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
      at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
      at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
      at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
      at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
      at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
      at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
      at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:165)
      at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
      at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
      at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
      at com.abc.bpe.core.support.CompositeJobLauncher.runJob(CompositeJobLauncher.java:207)
      at com.abc.des.bpe.core.support.CompositeJobLauncher$1.run(CompositeJobLauncher.java:119)
      at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
  Caused by: java.sql.SQLException: enlist: caught Exception
      at com.ibm.ws.rsadapter.AdapterUtil.toSQLException(AdapterUtil.java:1000)
      at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.beginTransactionIfNecessary(WSJdbcConnection.java:513)
      at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.prepareStatement(WSJdbcConnection.java:2373)
      at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.prepareStatement(WSJdbcConnection.java:2339)
      at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:238)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:638)
      ... 60 more
  Caused by: javax.resource.ResourceException: enlist: caught Exception
      at com.ibm.ejs.j2c.LocalTransactionWrapper.enlist(LocalTransactionWrapper.java:585)
      at com.ibm.ejs.j2c.ConnectionManager.lazyEnlist(ConnectionManager.java:1726)
      at com.ibm.ws.rsadapter.spi.WSRdbManagedConnectionImpl.lazyEnlist(WSRdbManagedConnectionImpl.java:2084)
      at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.beginTransactionIfNecessary(WSJdbcConnection.java:468)
      ... 64 more
  Caused by: java.lang.IllegalStateException: Illegal attempt to enlist multiple 1PC XAResources
      at com.ibm.tx.jta.impl.RegisteredResources.enlistResource(RegisteredResources.java:441)
      at com.ibm.tx.jta.impl.TransactionImpl.enlistResource(TransactionImpl.java:2252)
      at com.ibm.tx.jta.embeddable.impl.EmbeddableTranManagerSet.enlistOnePhase(EmbeddableTranManagerSet.java:202)
      at com.ibm.ws.transaction.services.TransactionManagerService.enlistOnePhase(TransactionManagerService.java:382)
      at com.ibm.ejs.j2c.LocalTransactionWrapper.enlist(LocalTransactionWrapper.java:501)
      ... 67 more
  </code></pre>
  
  <p>I added <code>@Transactional</code> to the InitializingTasklet.execute(...) method</p>
  
  <p>Any help regarding this issue is greatly appreciated. Also if any other code needed, please let know</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>serviceBeans.xml</p>
  
  <pre><code>    &lt;tx:advice id="txServiceAdvice" transaction-manager="txManager"&gt;
              &lt;tx:attributes&gt;
                  &lt;tx:method name="runJob" propagation="REQUIRED" rollback-for="Throwable" isolation="READ_COMMITTED" /&gt;
              &lt;/tx:attributes&gt;
          &lt;/tx:advice&gt;
  
  &lt;!-- commented the below line now and change the transactionManager--&gt;
          &lt;!-- &lt;bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
              &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;/bean&gt; --&gt;
  
          &lt;bean id="txManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager" /&gt;
  
          &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I removed multiple transaction managers and now only have the WebspereUOWTransactionManager</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>Additional code:</strong></p>
  
  <p><strong>BatchServiceImpl (Added @Transactional):</strong></p>
  
  <pre><code>@Override
      @Transactional(value = "transactionManager", rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
      public void launchJob(JobLaunchRequest request) throws ServiceException {
          try {
              logger.info("Launching job: " + request.getJobParameters().toString());
              jobLauncher.runAll(request.getJobParameters());
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  </code></pre>
  
  <p><strong>Code that calls the DAO:</strong></p>
  
  <pre><code>public class ParameterBasedCompositeJobLauncher implements CompositeJobLauncher, InitializingBean {
  public Collection&lt;JobExecution&gt; runAll(final Collection&lt;JobExecutionParameters&gt; jobParameterSet)
              throws JobExecutionAlreadyRunningException, JobRestartException,
              JobInstanceAlreadyCompleteException, JobParametersInvalidException {
  ....
  jobRunDao.updateJobStatus(batchId, jobRunStatus);
  ....
  }
  }
  </code></pre>
  
  <p><strong>ApplicationContext.xml</strong></p>
  
  <pre><code>&lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
          &lt;tx:attributes&gt;
              &lt;tx:method name="add*" propagation="REQUIRES_NEW" rollback-for="Exception" isolation="READ_COMMITTED" /&gt;
              &lt;tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="Exception" isolation="READ_COMMITTED" /&gt;
          &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  </code></pre>
  
  <p><strong>spring-dataSource.xml</strong></p>
  
  <pre><code>&lt;bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;      
  
      &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"
            c:dataSource-ref="dataSource" /&gt;
  </code></pre>
  
  <p><strong>spring-launch-context.xml</strong></p>
  
  <pre><code>&lt;bean id="jobLauncher" class="com.abc.core.support.ParameterBasedCompositeJobLauncher"&gt;
          &lt;property name="jobRepository" ref="jobRepository" /&gt;
          &lt;property name="taskExecutor" ref="taskExecutor"/&gt;
          &lt;property name="jobLocator" ref="jobRegistry" /&gt;
          &lt;property name="jobRunDao" ref="jobRunDao"/&gt;
          &lt;property name="forwardJobClient" ref="integration-forwardJobClient"/&gt;
          &lt;property name="responseFileGenerator" ref="responseFileGenerator"/&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor"&gt;
          &lt;property name="jobRegistry" ref="jobRegistry"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jobRepository"
            p:dataSource-ref="dataSource"
            p:transactionManager-ref="transactionManager"
            p:lobHandler-ref="lobHandler"
            p:tablePrefix="${SCHEMA}.BATCH_"
            p:isolationLevelForCreate="ISOLATION_READ_COMMITTED"
            class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"/&gt;
  </code></pre>