REQUIRES_NEW的传播也会回滚外部事务。为什么?

时间:2012-10-04 15:39:35

标签: spring transactions spring-transactions

我看到一种与文档不一致的奇怪行为。这就是我正在做的事情:从我的Test类中,我调用一个@Transactional服务,在DB中创建一行,然后调用另一个抛出RunTimeException的@Transactional服务(标记为REQUIRES_NEW)。我希望在我的测试结束时看到我的记录,但我不是。想法?

我的测试课程:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:**/jdbc-beans.xml","classpath:**/jdbc-infrastructure.xml","classpath:**/jdbc-infrastructure-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class TransactionalTest {

   @Autowired
   FeedManagerOne feedManagerOne;

   @Test
   public void test_propagation_REQUIRES_to_REQUIRES_NEW() {
      Feed anotherFeed = new Feed("AnotherRSS", "http://www.anotherfeedlink.com", true);
      assertFalse(feedManagerOne.exists(anotherFeed)); // EVALUATES TO FALSE

      try { 
         feedManagerOne.createFeed(anotherFeed, true);
      } catch (RuntimeException e) {
         e.printStackTrace();
      }

      // EVALUATES TO FALSE. WRONG! IT SHOULD BE TRUE.
      assertTrue(feedManagerOne.exists(anotherFeed)); 
   }
}

服务一:

@Service
public class FeedManagerOne {

   @Autowired
   FeedManagerTwo feedManagerTwo;

   @Autowired
   JdbcTemplate jdbcTemplate;

   @Transactional(readOnly = true)
   public boolean exists(Feed feed) {
      String query = "SELECT COUNT(*) FROM feed WHERE name = ? AND url = ? AND is_active = ?";
      int total = jdbcTemplate.queryForInt(query, feed.getName(), feed.getUrl(), feed.isActive());
      boolean found = (total == 1);
      return found;
   }

   @Transactional
   public boolean createFeed(Feed feed, boolean invokeFeedManagerTwo) {
      String query = "INSERT INTO feed (name, url, is_active) values (?, ?, ?)";
      int rowsChanged = jdbcTemplate.update(query, feed.getName(), feed.getUrl(), feed.isActive());
      boolean created = (rowsChanged == 1);

      // WHEN THE THE FOLLOWING METHOD THROWS A RUNTIME EXCEPTION,
      // THE JUST CREATED RECORD WILL BE LOST FROM THE DB (SEE TEST)
      feedManagerTwo.throwRuntimeExceptionFromRequiresNew();

      return created;
   }        
}

服务二

@Service
public class FeedManagerTwo {
   @Transactional(propagation = Propagation.REQUIRES_NEW,  isolation = Isolation.READ_COMMITTED)
   public void throwRuntimeExceptionFromRequiresNew() {
      if (true) {
         throw new RuntimeException("From REQUIRES_NEW");
      }
   }
}

1 个答案:

答案 0 :(得分:3)

这与文档完全一致。内部服务抛出异常,因此其事务将被回滚。然后异常传播到外部服务,这会导致外部事务也回滚。

在外部服务中捕获异常,它将不再回滚。

相关问题