Java Spring事务没有';异常后返回
我正在创建新用户,之后,当我通过Java Spring事务没有';异常后返回,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我正在创建新用户,之后,当我通过createProfileRequest()为用户创建配置文件时,服务将向我返回一个错误,用户不会被回滚 @Transactional(rollbackFor = {RestClientException.class, IllegalStateException.class}) public User createUser(UserRegistrationForm registrationForm) throws UserAlreadyExistsExce
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()
方法可见性。默认情况下,事务性批注仅适用于公共方法。使用@TransactionSynchronizationManager您使用的是哪个数据库?你能为相应的TransactionManager提供配置吗?我使用mysql,没有配置TransactionManager。我使用spring boot进行自动配置错误是什么意思?引发了什么类型的异常?RestClientException.class-服务器上处理数据时出错,IllegalStateException.class-当没有注册的服务时。那都是可能的。