D. *_*ski 9 java spring struts hibernate jpa
调用的方法:
1.Struts Action
2.服务类方法(由@Transactional注释)
3.Xfire webservice调用
包括struts(DelegatingActionProxy)和事务在内的所有东西都是用Spring配置的.
使用JPA/Hibernate完成持久化.
有时web服务会抛出一个未经检查的异常.我捕获此异常并抛出一个已检查的异常.我不希望事务回滚,因为Web服务异常会更改当前状态.我已经注释了这样的方法:
@Transactional(noRollbackFor={XFireRuntimeException.class, Exception.class})
public ActionForward callWS(Order order, ....) throws Exception
(...)
OrderResult orderResult = null;
try {
orderResult = webService.order(product, user)
} catch (XFireRuntimeException xfireRuntimeException) {
order.setFailed(true);
throw new WebServiceOrderFailed(order);
} finally {
persist(order);
}
}
Run Code Online (Sandbox Code Playgroud)
我仍然得到这个例外:
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
Run Code Online (Sandbox Code Playgroud)
当我尝试使用junit重现此事务时,事务未标记为回滚,并且仍可以提交事务.
如何让Spring不回滚事务?
管理为此问题创建测试用例:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
网络服务:
@Transactional(readOnly = true)
public class WebService {
public void letsThrowAnException() {
throw new XFireRuntimeException("test!");
}
}
Run Code Online (Sandbox Code Playgroud)
这将重新创建回滚异常.
然后我意识到事务可能在WebService.letsThrowAnException中被标记为rollbackOnly,因为WebService也是事务性的.我转到注释:
@Transactional(noRollbackFor={XFireRuntimeException.class})
public void letsThrowAnException() {
Run Code Online (Sandbox Code Playgroud)
现在事务没有回滚,我可以将更改提交给Order.
| 归档时间: |
|
| 查看次数: |
49439 次 |
| 最近记录: |