title: How to configure mutliple transaction managers with Spring + DBUnit + JUnit
body: <p><strong>In a nutshell</strong> </p>
  
  <p>My command line Java application copies data from one datasource to another without using XA. I have configured two separate datasources and would like a JUnit test that can rollback data on both datasources. I use DBUnit to load data into the "source" database, but I cannot get this to rollback. I can get the "target" datasource to rollback.</p>
  
  <p><strong>My Code</strong></p>
  
  <p>Given this config...</p>
  
  <pre><code>&lt;tx:annotation-driven /&gt;
  
  &lt;!-- note the default transactionManager name on this one --&gt;
  &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource"     ref="dataSourceA" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManagerTarget" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource"     ref="dataSourceB" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and this code...</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"classpath:resources/spring-context.xml",
                                  "classpath:resources/spring-db.xml"})  
  @Transactional
  @TransactionConfiguration(transactionManager = "transactionManagerTarget", defaultRollback = true) 
  public class MyIntegrationTest {
  
      @Autowired
      private MyService service;
  
      @Autowired
      @Qualifier("dataSourceA")
      private DataSource dataSourceA;
  
      private IDataSet loadedDataSet;
  
      /**
       * Required by DbUnit
       */
      @Before
      public void setUp() throws Exception {
          SybaseInsertIdentityOperation.TRUNCATE_TABLE.execute(getConnection(), getDataSet());
          SybaseInsertIdentityOperation.INSERT.execute(getConnection(), getDataSet());
      }
  
      /**
       * Required by DbUnit
       */
      protected IDataSet getDataSet() throws Exception {
          loadedDataSet = DbUnitHelper.getDataSetFromFile(getConnection(), "TestData.xml");
          return loadedDataSet;
      }
  
      /**
       * Required by DbUnit
       */
      protected IDatabaseConnection getConnection() throws Exception{
          return new DatabaseConnection(dataSourceA.getConnection());
      }   
  
      @Test
      public void testSomething() {
  
          // service.doCopyStuff();
  
      }
  
  }
  </code></pre>
  
  <p>The problem as I see it, is that <code>@TransactionConfiguration</code> only states the target datasource for enabling a rollback. DBUnit is being passed <code>dataSourceA</code> explicitly and is picking up the default transaction manager named <code>transactionManager</code> (I'm not sure how) which has not been told to rollback.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I tell both transaction managers to rollback?</p>
  
  <p>Can I use a single transaction manager when my datasources do not support XA transactions?</p>
  
  <p><em>Note: The application does not require a transaction manager on dataSourceA when running in production as it will only be read-only. This issue is for my tests classes only.</em></p>