1

Project in question uses Spring 4, Java 7, and persists to Oracle and PostgreSQL.

I'm writing integration tests for a service that persists data to both databases. For the integration test, I want to have both transactions rollback automatically. I know I can't use "repeated" annotations if not using Java 8, as I'll get a compile error. I can compile just fine if I put one annotation at the class level, and another at the method level. However, it appears that the higher annotation listed will not rollback. To clarify, if I declare either @Transactional annotation (Oracle or PostgreSQL) as a standalone annotation at either the class or method level, that one rollback works perfectly. But if I implement one annotation at the class level, and the other annotation at the method level, the rollback will only work for the method level and not the class level.

I suspect multiple @Transactional annotations would work just fine as long as the process is only trying to rollback one at a time. Ex: One method uses the Oracle Transaction and a different method uses the PostgreSQL Transaction.

Any thoughts?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
user2970100
  • 111
  • 1
  • 3

1 Answers1

5

OK, I figured out how to do it. There is a separate SpringFramework project "spring-data-commons" that has a class named ChainedTransactionManager. I wired up a bean as follows:

<bean id="chainedTransactionManager"
          class="org.springframework.data.transaction.ChainedTransactionManager">
        <constructor-arg>
            <list>
                <ref bean="postgresqlTransactionManager"/>
                <ref bean="oracleTransactionManager"/>
            </list>
        </constructor-arg>
    </bean>

Then I used the chainedTransactionManager for my annotation

@Transactional(transactionManager = "chainedTransactionManager")

Both transactions rolled back as expected after making this change.

user2970100
  • 111
  • 1
  • 3