My signupUser method saves a user and sends an email to the registered email address. If the email fails to be sent, the user data saved has to be rolled back. I've added @Transactional
on the signupUser method but it doesn't roll back when an exception happens and the data gets permanently saved. I temporarily replaced the sending email part with simply throw RunTimeException
to check if transactional would work but the result was the same (didn't roll back).
This method is called in a @Controller
annotated class.
What am I doing wrong?
@Service
public class AuthService {
private UserRepository userRepository;
private final Logger logger = LoggerFactory.getLogger(AuthService.class);
private CustomMailSender mailSender;
private ModelMapper modelMapper;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
public AuthService(UserRepository userRepository, CustomMailSender mailSender, ModelMapper modelMapper) {
this.userRepository = userRepository;
this.mailSender= mailSender;
this.modelMapper = modelMapper;
}
@Transactional
public void signupUser(SignUpForm signUpForm){
userRepository.save(new User(signUpForm.getUsername(), passwordEncoder.encode(signUpForm.getPassword()), signUpForm.getEmail()));
throw new RuntimeException("腹たつ");
}
I found out it was the configuration on mySQL. I changed the engine from the default one to InnoDB and it worked!