I wrote a simple program that i want to see the result of transaction rollback like this:

@Transactional
public void insertTwo() throws Exception {
    girlRepository.save(new Girl(18, "B"));
    girlRepository.save(new Girl(18, "A"));
    throw new RuntimeException("e");
}

but there were still two new record what I added into MySQL database. It showed that the rollback happened in the spring-boot application but not in MySQL. The version of spring-boot is 2.0.0, btw. But then I tested with the h2 database. The rollback worked in h2 database. Then I tested when the version of spring-boot is 1.5.2 and used MySQL. The rollback worked in MySQL, too. I want to know why I used the same code but got diffrent answer.

Comment From: geo-m

Try @Transactional(rollbackOn = {Exception.class})

And put the annotation on a @Service class method (if its on a controller now)

Comment From: SkyeBeFreeman

@geo-m still cannot rollback the new record in MySQL. And it is "rollbackFor", not "rollbackOn", in spring-boot 2.0, btw : )

Comment From: geo-m

And it is "rollbackFor", not "rollbackOn", in spring-boot 2.0, btw : )

Using javax.transaction annotation. Doubt it had anything to do with it working though, sorry

Comment From: SkyeBeFreeman

@geo-m I tried javax.transaction annotation but it still cannot work : (

Comment From: murilolocatelli

@SkyeBeFreeman Did you discover the problem? I had same problem when I updated spring boot to 2.0.0

Comment From: SkyeBeFreeman

@murilolocatelli did you config the engine of mysql as InnoDB?

Comment From: murilolocatelli

@SkyeBeFreeman When I updated to 2.0.0 stoped to use InnoDB. I configured spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect and it worked. Thanks =)

Comment From: Anky723

@SkyeBeFreeman I have tried the above solution but it not working(Using 2.0.0 and MySQL5InnoDBDialect). I am trying to save data in multiple tables one by one. If any save fails the previous save is not rolled back.

Comment From: hoangld17

I have tried all solution above, but I still fail!