Java Spring jpa在回滚期间保存数据
我知道这个问题听起来很奇怪,但道理很简单。 我用Spring+Hibernate开发了一个服务器端应用程序 我有一个自定义servlet:Java Spring jpa在回滚期间保存数据,java,spring,hibernate,jpa,spring-data,Java,Spring,Hibernate,Jpa,Spring Data,我知道这个问题听起来很奇怪,但道理很简单。 我用Spring+Hibernate开发了一个服务器端应用程序 我有一个自定义servlet: @Transactional @Component public class ReceivingSms implements HttpRequestHandler { ... ... ... @Override public void handleRequest(HttpServletRequest request, HttpServletRespo
@Transactional
@Component
public class ReceivingSms implements HttpRequestHandler {
...
...
...
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
....reading data from parameters and store into the db....
entityManager.persist(someEntities);
//at some point I found that there is some error and I've to rollback the entire transaction
if(error){
//HERE I WANT TO SAVE A LOG ON DB
throw new RuntimeException("Error");
}
}
原因是:
- 我正在读取到达servlet的参数
- 我将从这些参数导出的一些数据存储在数据库中
- 在某种程度上,我知道有一个错误,我无法完成这个过程。所以我抛出一个RuntimeException来回滚整个事务
- 在返回之前,我想在数据库中保存一个特定的实体(一种日志)。但是,如果我尝试,这个persist将从RuntimeException回滚
谢谢调用另一个事务服务,它的方法用
@Transactional(propagation = Propagation.REQUIRES_NEW)
保存日志条目