title: Rollback transaction in functional tests started in another spring context
body: <p>'m implementing functional tests. To avoid data corruption I need to rollback every test after execution. This is pretty trivial task - just mark rollback=true in test. But if I start another module with another spring context and first module interacts with it somehow (f.e. sends jms message and second one saves it to the same DB) then rollback is not working for second context. How to rollback second module too?</p>
  
  <pre><code>    @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:/my-service-context.xml")
  @Transactional(value = "myTransactionManager")
  public class ParserServiceTest {
  
      protected Logger l = LoggerFactory.getLogger(getClass());
  
      @Autowired
      @Qualifier(value = "earMessageDaoBean")
      EARMessageDao dao;
  
      @Autowired
      @Qualifier(value = "myParserService")
      ParserService service;
  
      @Test
      @Rollback(value = true)
      public void testExecute() throws Exception {
          service.execute("fff", "ttt");
          EARMessage byId = dao.findById(1L);
          assertNotNull(byId);
          assertEquals("fff", byId.getFrom());
          assertEquals("ttt", byId.getTo());
          l.info("{}", byId);
      }
  }
  </code></pre>
  
  <p>if I take a look into db i will see no data and it's good</p>
  
  <p>but if I will add another module</p>
  
  <pre><code>    @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:/my-service-context.xml")
  @Transactional(value = "myTransactionManager")
  public class ParserServiceTest {
  
      protected Logger l = LoggerFactory.getLogger(getClass());
  
      ClassPathXmlApplicationContext context2;
  
      @Before
      public void setUp() throws Exception {
          context2 = new ClassPathXmlApplicationContext(
                  "my-service-context2.xml"
          );
      }
  
      @Autowired
      @Qualifier(value = "earMessageDaoBean")
      EARMessageDao dao;
  
      @Autowired
      @Qualifier(value = "myParserService")
      ParserService service;
  
      @Test
      @Rollback(value = true)
      public void testExecute() throws Exception {
          service.execute("fff", "ttt");
          EARMessage byId = dao.findById(1L);
          assertNotNull(byId);
          assertEquals("fff", byId.getFrom());
          assertEquals("ttt", byId.getTo());
          l.info("{}", byId);
  // theoretically there could be interaction with service2 via JMS
          MyParserService2 service2 = (MyParserService2) context2.getBean("myParserService2");
          service2.execute("FFF", "TTT");
      }
  }
  </code></pre>
  
  <p>data added by servie2 will not be rollbacked.</p>
  
  <p>I can get EntityManager TransactionManager within test context but I can't roll them back since transactions are commited already.
  I can add some marker into second module and mark it in test somehow but have now idea yet what to do</p>
  
  <p>UPDATE
  here are services and context configs for better understanding:</p>
  
  <pre><code>    @Service(value = "myParserService")
  @Transactional(value = "myTransactionManager")
  public class ParserService {
  
      protected Logger l = LoggerFactory.getLogger(getClass());
  
      @Autowired
      @Qualifier(value = "earMessageDaoBean")
      EARMessageDao dao;
  
      public void execute(String from, String to) {
          l.info("-------started service 1---------");
          EARMessage message = new EARMessage();
          message.setFrom(from);
          message.setTo(to);
          message.setProcessingDate(new DateTime());
          dao.persist(message);
          l.info("-------ended  service 1---------");
      }
  }
  
  
      @Service(value = "myParserService2")
  @Transactional(value = "myTransactionManager")
  public class MyParserService2 {
  
      protected Logger l = LoggerFactory.getLogger(getClass());
  
      @Autowired
      @Qualifier(value = "earMessageDaoBean")
      EARMessageDao dao;
  
      public void execute(String from, String to) {
          l.info("-------started service 2---------");
          EARMessage message = new EARMessage();
          message.setFrom("666" + from);
          message.setTo("666" + to);
          message.setProcessingDate(new DateTime());
          dao.persist(message);
          l.info("-------ended  service 2---------");
      }
  }
  </code></pre>
  
  <p>my-service-context.xml</p>
  
  <pre><code>   &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="com.dimas.tutorial.hibernate.simple"/&gt;
  
  &lt;import resource="classpath:/my-service-config.xml"/&gt;
  &lt;import resource="classpath:/my-data-source.xml"/&gt;
  &lt;import resource="classpath:/my-entity-manager.xml"/&gt;
  
  &lt;jdbc:initialize-database data-source="${dataSource.name}"&gt;
      &lt;jdbc:script location="classpath:/sql/my-schema.sql"/&gt;
  &lt;/jdbc:initialize-database&gt;
  </code></pre>
  
  <p>my-service-context2.xml</p>
  
  <pre><code>   &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="com.dimas.tutorial.hibernate.simple"/&gt;
  
      &lt;import resource="classpath:/my-service-config.xml"/&gt;
      &lt;import resource="classpath:/my-data-source.xml"/&gt;
      &lt;import resource="classpath:/my-entity-manager.xml"/&gt;
  
      &lt;jdbc:initialize-database data-source="${dataSource.name}"&gt;
          &lt;jdbc:script location="classpath:/sql/my-schema.sql"/&gt;
      &lt;/jdbc:initialize-database&gt;
  </code></pre>
  
  <p>UPDATE2: added entity manager config</p>
  
  <pre><code>&lt;bean id="valettaEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="MyPersistence"/&gt;
          &lt;property name="packagesToScan" value="com.dimas.tutorial.hibernate.simple.domain"/&gt;
          &lt;property name="dataSource" ref="${dataSource.name}"/&gt;
          &lt;property name="jpaVendorAdapter" ref="hibernateVendor"/&gt;
          &lt;property name="jpaPropertyMap" ref="jpaPropertyMap"/&gt;
      &lt;/bean&gt;
  
      &lt;util:map id="jpaPropertyMap"&gt;
          &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
          &lt;entry key="hibernate.default_schema" value="public"/&gt;
          &lt;entry key="hibernate.show_sql" value="false"/&gt;
          &lt;entry key="hibernate.format_sql" value="false"/&gt;
          &lt;entry key="hibernate.cache.use_second_level_cache" value="false"/&gt;
          &lt;entry key="hibernate.max_fetch_depth" value="3"/&gt;
          &lt;entry key="hibernate.jdbc.fetch_size" value="50"/&gt;
          &lt;entry key="hibernate.jdbc.batch_size" value="10"/&gt;
      &lt;/util:map&gt;
  
      &lt;bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="showSql" value="false"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="valettaEntityManagerFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="valettaTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"&gt;
          &lt;property name="transactionManager" ref="myTransactionManager"/&gt;
      &lt;/bean&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;tx:annotation-driven transaction-manager="myTransactionManager"/&gt;
  </code></pre>