我看到一种与文档不一致的奇怪行为。这就是我正在做的事情:从我的测试类中,我调用一个@Transactional 服务,该服务在数据库中创建一行,然后调用另一个引发 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");
}
}
}