【问题标题】:JPA-Repository read access commits outer transactionJPA-Repository 读取访问提交外部事务
【发布时间】:2017-11-14 17:00:57
【问题描述】:

我对 Springs DB-Transaction 处理有一个奇怪的行为,我无法理解。

给定以下代码:

    @Transactional
    public void saveDocumentToDB() {

        dbService.addNewEntity('123', 'helloWorld');
        DbEntity dbEntity = dbService.getEntityByID('someId');

        throw new RuntimeException();
    }

其中dbService 是一些@Service-class,可以访问JPA-CrudRepository。

我的问题:getEntityByID-Method 反过来调用自动生成的存储库对象上的方法提交事务,从而将第一个“添加”调用写入数据库。

没有这个调用,事务会很好地回滚...

您对如何使自动生成的 CRUD 方法也尊重事务有任何建议吗?

【问题讨论】:

    标签: java spring hibernate transactions spring-data-jpa


    【解决方案1】:

    在我的 application.properties 中,我使用了

    spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

    这导致 Hibernate 使用不支持事务(和外键)的 MyISAM-Engine 生成其表。

    我改成

    spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    

    并重建 db-schema。

    我觉得这很不直观,所谓的默认“MySQL5Dialect”不使用默认引擎,而是使用 MyISAM。

    也许它对将来的某人有帮助。

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 1970-01-01
      • 2015-03-15
      • 2011-03-13
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 2015-12-17
      • 2016-10-23
      相关资源
      最近更新 更多