One: Add comments to the Springboot startup class:@EnableTransactionManagement
@EnableDiscoveryClient @springbootapplication@enablefeignclients@enabletransactionmanagement Public class deploymentserviceapplication { publicstaticvoid main (string[] args) { Springapplication.run (deploymentserviceapplication. class , args);} }
Two: Add the annotation in the corresponding place: @Transactional can
@Service Public classDemoserviceimpl implements Demoservice {@Autowired personrepository personrepository; @Transactional (Rollbackfor= {IllegalArgumentException.class}) @Override PublicPerson savepersonwithrollback (person person) {person P=Personrepository.save (person); if(Person.getname (). Equals ("sang")) { Throw NewIllegalArgumentException ("sang already exists, data will be rolled back"); } returnp; } @Transactional (Norollbackfor= {IllegalArgumentException.class}) @Override PublicPerson savepersonwithoutrollback (person person) {person P=Personrepository.save (person); if(Person.getname (). Equals ("sang")) { Throw NewIllegalArgumentException ("sang already exists, but data is not rolled back"); } returnp; }}
Using Database transactions in spring boot