我正在创建新用户,此后,当我通过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()
方法可见性。默认情况下,事务注释仅适用于公共方法。