简体   繁体   English

Spring Boot @Transactional 注释不起作用

[英]Spring Boot @Transactional annotation doesn't work

My signupUser method saves a user and sends an email to the registered email address.我的 signupUser 方法保存用户并向注册的电子邮件地址发送电子邮件。 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.我在 signupUser 方法上添加了@Transactional ,但是当发生异常并且数据被永久保存时它不会回滚。 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).我暂时用简单的throw RunTimeException替换了发送电子邮件的部分,以检查事务是否有效,但结果是一样的(没有回滚)。

This method is called in a @Controller annotated class.这个方法在@Controller注释类中调用。

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("腹たつ");

        }

Hmm, IMO you should use import org.springframework.transaction.annotation.Transactional;嗯,IMO 你应该使用import org.springframework.transaction.annotation.Transactional; bcuz you're using spring, not EE因为你使用的是 spring,而不是 EE

I found out it was the configuration on mySQL.我发现它是 mySQL 上的配置。 I changed the engine from the default one to InnoDB and it worked!我将引擎从默认引擎更改为 InnoDB,并且成功了!

Spring boot @Transactional doesn't rollback Spring boot @Transactional 不回滚

If the annotation did not work you can try an alternative way for the same, modify your service class like the below code如果注释不起作用,您可以尝试另一种方法,修改您的服务类,如下面的代码

public void signupUser(SignUpForm signUpForm){
        userRepository.add(signUpForm.getUsername(), passwordEncoder.encode(signUpForm.getPassword()), signUpForm.getEmail());
        throw new RuntimeException("腹たつ");

        }

Add the below code into Repository将以下代码添加到 Repository 中

@Transactional
@Modifying
@Query(value="insert into..... ?1 ?2 ?3",nativeQuery=true)
void add(name,password,email);

so from your service just call the method add passing 3 parameters and here ?1, ?2 will be replaced with name password etc.. try with this and it will work因此,从您的服务中只需调用该方法添加传递 3 个参数,此处 ?1、?2 将被替换为名称密码等。尝试使用此方法即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM