gpt4 book ai didi

java - Spring事务在异常后不回滚

转载 作者:行者123 更新时间:2023-11-30 05:41:13 24 4
gpt4 key购买 nike

我正在创建新用户,之后当我通过createProfileRequest()为用户创建配置文件并且服务检索到错误时,用户不会被回滚

@Transactional(rollbackFor = {RestClientException.class, IllegalStateException.class})
public User createUser(UserRegistrationForm registrationForm) throws UserAlreadyExistsException {
if (userRepository.existsUserByEmailOrUsername(registrationForm.getEmail(), registrationForm.getUsername()))
throw new UserAlreadyExistsException("User with credentials " +
registrationForm.getEmail() + "/" + registrationForm.getUsername() + " already exists");

User user = new User(registrationForm.getUsername(), registrationForm.getEmail());
user.setPassword(passwordEncoder.encode(registrationForm.getPassword()));
user.setRoles(getDefaultRoles());

userRepository.save(user);

createProfileRequest(user);

tokenService.createToken(user.getId(), TokenType.ACTIVATION);
log.info("Created user with id: " + user.getId());
return user;
}

private void createProfileRequest(User user) throws RestClientException, IllegalStateException {
ObjectNode profile = new ObjectMapper().createObjectNode();
profile.put("userId", user.getId());
profile.put("visibleName", user.getUsername());
restTemplate.postForLocation("http://profile/api", profile);
}

最佳答案

我认为您的问题在于此处的 createProfileRequest() 方法可见性。默认情况下,事务注释仅适用于公共(public)方法。

关于java - Spring事务在异常后不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55617909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com