Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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:如何在一个请求中处理多个Hibernate事务?_Java_Spring_Hibernate_Transactions - Fatal编程技术网

Java:如何在一个请求中处理多个Hibernate事务?

Java:如何在一个请求中处理多个Hibernate事务?,java,spring,hibernate,transactions,Java,Spring,Hibernate,Transactions,我不确定在哪里打开我的事务对象。在服务层内部?还是控制器层 我的Controller基本上有两个服务,我们把它们称为AService和BService。然后我的代码是这样的: public class Controller { public AService aService = new AService(); public BService bService = new BService(); public void doSomething(SomeData data

我不确定在哪里打开我的
事务
对象。在服务层内部?还是控制器层

我的
Controller
基本上有两个服务,我们把它们称为
AService
BService
。然后我的代码是这样的:

public class Controller {
    public AService aService = new AService();
    public BService bService = new BService();

    public void doSomething(SomeData data) {
        //Transaction transaction = HibernateUtil.getSession().openTransaction();
        if (data.getSomeCondition()) {
            aService.save(data.getSomeVar1());
            bService.save(data.getSomeVar2());
        }
        else {
            bService.save(data.getSomeVar2());
        }


        //transaction.commit(); or optional try-catch with rollback 
    }
}
我想要的行为是,如果
bService#save
失败,那么我可以调用
事务#rollback
,以便
aService
中保存的任何内容也将回滚。这似乎只有在我为两次保存创建一个事务时才可能实现

但是从另一个角度来看,我的
控制器
依赖于
事务
,这看起来非常糟糕。如果我在各自的服务中创建
事务
(类似于Spring@Transactional的工作方式),那就更好了,但是如果我这样做,那么我不知道如何实现我想要的结果


编辑:修复代码,添加另一个条件。我没有使用任何Spring依赖项,因此@Transactional的使用是不可能的。

您可以通过另一层抽象和使用组合来完成您的要求

public class CompositeABService {
  @Autowired
  private AService aservice;
  @Autowired
  private BService bservice;

  @Transactional
  public void save(Object value1, Object value2) {
    aservice.save( value1 );
    bservice.save( value2 );
  }
}

public class AService {
  @Transactional
  public void save(Object value) {
    // joins an existing transaction if one exists, creates a new one otherwise.
  }
}

public class BService {
  @Transactional
  public void save(Object value) {
    // joins an existing transaction if one exists, creates a new one otherwise.
  }
}
当您需要作为单个工作单元(例如事务)的一部分与多个存储库交互时,通常会使用相同的模式


现在,您的控制器需要依赖的是
compositeaservice
或您想命名的任何东西。

好的-可能有点离题,但我没有使用任何Spring依赖项,所以我只是手动创建新事务。如果我在CompositeABService.save()中打开一个事务-这意味着在AService.save()和BService.save()中打开另一个事务将导致错误。请看我的编辑