How to configure mutliple transaction managers wit

2019-03-29 11:48发布

问题:

In a nutshell

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.

My Code

Given this config...

<tx:annotation-driven />

<!-- note the default transactionManager name on this one -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource"     ref="dataSourceA" />
</bean>

<bean id="transactionManagerTarget" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource"     ref="dataSourceB" />
</bean>

and this 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();

    }

}

The problem as I see it, is that @TransactionConfiguration only states the target datasource for enabling a rollback. DBUnit is being passed dataSourceA explicitly and is picking up the default transaction manager named transactionManager (I'm not sure how) which has not been told to rollback.

Question

How can I tell both transaction managers to rollback?

Can I use a single transaction manager when my datasources do not support XA transactions?

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.

回答1:

A possible workaround would be to introduce a helper bean annotated as @Transactional("transactionManagerTarget") and leave your test annotated as @Transactional("transactionManager"), configuring both with defaultRollback = true. Your test would then have to call the helper bean, which in turn would call your service bean under test. This should cause the transaction around your service to roll back, then the transaction around DBUnit.

It's a bit messy, though.

Other possible approaches:

  • Using an in-memory database such as H2 instead of your production database- you could configure this to drop all of its data when required.
  • Allow DBUnit to commit, and have a compensating transaction in your tear-down method to clear the data out.


回答2:

Use the <qualifier> element inside your transaction manager definition.

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceA" />
    <qualifier value="transactionManager" />
</bean>

<bean id="transactionManagerTarget" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceB" />
    <qualifier value="transactionManagerTarget" />
</bean>

Then you can reference which one you want to use directly in the @Transactional annotation, i.e.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:resources/spring-context.xml",
                                "classpath:resources/spring-db.xml"})  
@Transactional("transactionManagerTarget")
@TransactionConfiguration(defaultRollback = true) 
public class MyIntegrationTest {
...


回答3:

I have used XA transactions and rollbacks in JUnit tests using the open source TM Atomikos. One nice feature is that Atomikos allows using non-XA enabled data sources to participate in XA transactions. Check this link out for an example: http://www.atomikos.com/Documentation/NonXaDataSource

On the other hand, if XA is a decent solution for your JUnit issues is another story. Do your tests focus a lot on the database implementation (Sybase) or is it more about Java logic? I usually setup embedded DBs like Apache Derby or HQSQL for JUnit tests. Then I do not have to care much about clean ups, since GC will handle that :)