JPA save with multiple entities not rolling back when inside Spring @Transactional and rollback for Exception.class enabled

前端 未结 1 1681
庸人自扰
庸人自扰 2020-12-11 22:52

I have been at this for a good few hours and I can not find a way to get around what seems to be JPARepository automatically starting/committing a transaction when I save en

相关标签:
1条回答
  • 2020-12-11 23:05

    MySQL historically by default created tables using the MyISAM engine, which didn't support transactions. Later InnoDB was added which did support transactions, however MyISAM remained the default for quite some time.

    Hibernate with its dialects followed the same defaults and hence the default for the MySQL dialect is MyISAM (for backwards compatibility).

    You have 2 ways to fix this, either select a specific dialect or toggle from MyISAM to InnoDB with a specific property.

    To select a dialect use the spring.jpa.database-platform to specify the required dependency.

    spring.jpa.database-platform=org.hibernate.dialect.MySQL57InnoDBDialect`
    

    or to set the property use

    spring.jpa.properties.hibernate.dialect.storage_engine=innodb
    

    or a combination of both (as the MySQL57InnoDBDialect is deprecated now that there is the hibernate.dialect.storage_engine property).

    spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
    spring.jpa.properties.hibernate.dialect.storage_engine=innodb
    

    See also: https://in.relation.to/2017/02/20/mysql-dialect-refactoring/

    0 讨论(0)
提交回复
热议问题