使用@Async嵌套@Transactional方法

时间:2015-03-25 14:26:50

标签: java spring jpa spring-transactions

我正在使用Spring和JPA。我已启用@EnableAsync@EnableTransactionManagement。在我的用户注册服务方法中,我有一些我称之为注释@Async的其他服务方法。这些方法可以执行各种操作,例如发送欢迎电子邮件以及使用我们的第三方支付系统注册新创建的用户。

在我想验证第三方支付系统是否成功创建用户之前,一切正常。此时,@Async方法会尝试创建UserAccount(引用新创建的User)并使用javax.persistence.EntityNotFoundException: Unable to find com.dk.st.model.User with id 2017

创建错误

注册调用如下所示:

private User registerUser(User newUser, Boolean waitForAccount) {
    String username = newUser.getUsername();
    String email = newUser.getEmail();

    // ... Verify the user doesn't already exist

    // I have tried all manner of flushing and committing right here, nothing works
    newUser = userDAO.merge(newUser);

    // Here is where we register the new user with the payment system.
    // The User we just merged is not /actually/ in the DB
    Future<Customer> newCustomer = paymentService.initializeForNewUser(newUser);
    // Here is where I occasionally (in test methods) pause this thread to wait
    // for the successful account creation.
    if (waitForAccount) {
        try {
            newCustomer.get();
        } catch (Exception e) {
            logger.error("Exception while creating user account!", e);
        }
    }

    // Do some other things that may or may not be @Aysnc

    return newUser;
}

支付服务呼吁完成注册用户的工作,如下所示:

@Async
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Future<Customer> initializeForNewUser(User newUser) {
    // ... Set up customerParams

    Customer newCustomer = null;
    try {
        newCustomer = Customer.create(customerParams);

        UserAccount newAccount = new UserAccount();
        newAccount.setUser(newUser);
        newAccount.setCustomerId(newCustomer.getId());
        newAccount.setStatus(AccountStatus.PRE_TRIAL);

        // When merging, JPA cannot find the newUser object in the DB and complains
        userAccountDAO.merge(newAccount);
    } catch (Exception e) {
        logger.error("Error while creating UserAccount!", e);
        throw e;
    }

    return new AsyncResult<Customer>(newCustomer);
}

列出的StackOverflow答案here表示我设置了REQUIRES_NEW传播,我已经完成了,但没有运气。

有人能指出我正确的方向吗?我真的不想直接从我的控制器方法调用paymentService。我觉得这应该是一个服务水平的电话肯定。

感谢您的帮助!

2 个答案:

答案 0 :(得分:6)

在Vyncent的帮助下,这是我到达的解决方案。我创建了一个名为UserCreationService的新类,并将所有处理User创建的方法放在该类中。这是一个例子:

@Override
public User registerUserWithProfileData(User newUser, String password, Boolean waitForAccount) {
    newUser.setPassword(password);
    newUser.encodePassword();
    newUser.setJoinDate(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime());

    User registered = userService.createUser(newUser);
    registered = userService.processNewRegistration(registered, waitForAccount);

    return userService.setProfileInformation(registered);
}

您会注意到此方法有 NO @Transactional注释。这是故意的。相应的createUserprocessNewRegistration定义如下所示:

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public User createUser(User newUser) {
    String username = newUser.getUsername();
    String email = newUser.getEmail();

    if ((username != null) && (userDAO.getUserByUsername(username) != null)) {
        throw new EntityAlreadyExistsException("User already registered: " + username);
    }

    if (userDAO.getUserByUsername(newUser.getEmail()) != null) {
        throw new EntityAlreadyExistsException("User already registered: " + email);
    }

    return userDAO.merge(newUser);
}

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public User processNewRegistration(
        User newUser,
        Boolean waitForAccount) 
{
    Future<UserAccount> customer = paymentService.initializeForNewUser(newUser);
    if (waitForAccount) {
        try {
            customer.get();
        } catch (Exception e) {
            logger.error("Error while creating Customer object!", e);
        }
    }

    // Do some other maintenance type things...

    return newUser;
}

Vyncent认为交易管理是个问题。创建其他服务使我能够更好地控制何时提交这些事务。虽然我最初对采用这种方法犹豫不决,但这是与Spring托管交易和代理的权衡。

我希望这可以帮助别人以后节省一些时间。

答案 1 :(得分:3)

尝试创建一个新的 UserService 类来管理用户检查,如此

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public User createOrUpdateUser(User newUser) {
    String username = newUser.getUsername();
    String email = newUser.getEmail();

    // ... Verify the user doesn't already exist

    // I have tried all manner of flushing and committing right here, nothing works
    newUser = userDAO.merge(newUser);
    return newUser;
}

然后在实际课程中,改变

private User registerUser(User newUser, Boolean waitForAccount) {
    String username = newUser.getUsername();
    String email = newUser.getEmail();

    // ... Verify the user doesn't already exist

    // I have tried all manner of flushing and committing right here, nothing works
    newUser = userDAO.merge(newUser);

通过

private User registerUser(User newUser, Boolean waitForAccount) {
    newUser = userService.createOrUpdateUser(newUser);

带有 @Transactional REQUIRES_NEW 的新userService应该强制提交并解决问题。