@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"file:web/WEB-INF/spring/applicationContext.xml", "file:web/WEB-INF/spring/services.xml"})@Transactionalpublic class DonotRollBackTest { @Autowired FakeService fakeService; @Test @Rollback(false) public void testRunXFireException() { fakeService.doSomeTransactionalStuff(); }}
FakeService:
@Servicepublic 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。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)