【问题标题】:Transaction between several DAO layers?几个 DAO 层之间的事务?
【发布时间】:2010-12-10 17:38:29
【问题描述】:

如下所示,我正在访问另一个 DAO 内部的服务层方法。 (系统中的每一个DAO都是使用HibernateDAOSupport类实现的)

我想在#1 或#2(在下面的代码中注释)失败时回滚事务。 但是当#2 抛出异常时,#1 不会回滚,我可以看到数据库中的条目。

@Transactional(readOnly=false, rollbackFor={DuplicateEmailException.class,DuplicateLoginIdException.class,IdentityException.class},propagation=Propagation.REQUIRES_NEW)
    public void createUserProfile(UserProfile profile)
            throws DuplicateEmailException, DuplicateLoginIdException,
            IdentityException {

        // #1 create principal using Identity Service
        identityService.createPrincipal(profile.getSecurityPrincipal());

        try {
        // #2 save user profile using Hibernate Template
            getHibernateTemplate().save(profile);
        } catch (RuntimeException e) {
            throw new IdentityException("UseProfile create Error", e);
        }

}

这里是'IdentityService'的createPrincipal()方法的签名。

public void createPrincipal(Principal principal) throws DuplicateEmailException,DuplicateLoginIdException,IdentityException ;

“IdentityService”中没有配置事务管理

我在这里做错了什么?

【问题讨论】:

  • identityService.createPrincipal(...) 方法必须创建它自己的事务。它的 DAO 是如何配置的?
  • 那个DAO没有为任何事务配置。(没有事务注释)。
  • 我使用过'Transactional'注解的唯一地方是在DAO上面。

标签: java hibernate spring transactions dao


【解决方案1】:

试试Propagation.REQUIRED,而不是Propagation.REQUIRES_NEW

【讨论】:

    【解决方案2】:

    在通话期间identityService.createPrincipal(profile.getSecurityPrincipal()); 你不是在刷新会话吗? (例如,使用 FlushMode.AUTO 执行查询)

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 2013-05-16
      • 2011-08-14
      • 2013-05-27
      • 2014-08-15
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多