Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring jpa在回滚期间保存数据_Java_Spring_Hibernate_Jpa_Spring Data - Fatal编程技术网

Java Spring jpa在回滚期间保存数据

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

我知道这个问题听起来很奇怪,但道理很简单。 我用Spring+Hibernate开发了一个服务器端应用程序

我有一个自定义servlet:

@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)
保存日志条目