设法为这个问题创建了一个测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:web/WEB-INF/spring/applicationContext.xml",
"file:web/WEB-INF/spring/services.xml"})
@Transactional
public class DoNotRollBackTest {
@Autowired FakeService fakeService;
@Test
@Rollback(false)
public void testRunXFireException() {
fakeService.doSomeTransactionalStuff();
}
}
FakeService:
@Service
public class FakeService {
@Autowired private EcomService ecomService;
@Autowired private WebService webService;
@Transactional(noRollbackFor={XFireRuntimeException.class})
public void doSomeTransactionalStuff() {
Order order = ecomService.findOrderById(459);
try {
webService.letsThrowAnException();
} catch (XFireRuntimeException e) {
System.err.println("Caugh XFireRuntimeException:" + e.getMessage());
}
order.setBookingType(BookingType.CAR_BOOKING);
ecomService.persist(order);
}
}
网络服务:
@Transactional(readOnly = true)
public class WebService {
public void letsThrowAnException() {
throw new XFireRuntimeException("test!");
}
}
这将重新创建回滚异常。
然后我意识到该事务可能在WebService.letsThrowAnException中被标记为rollbackOnly,因为WebService也是事务性的。我移至注释:
@Transactional(noRollbackFor={XFireRuntimeException.class})
public void letsThrowAnException() {
现在,交易没有被回滚,我可以将更改提交到Order。