【问题标题】:how to make auto commit a jpa transaction如何使自动提交 jpa 事务
【发布时间】:2020-02-11 09:37:32
【问题描述】:

我有一个带有 Spring Boot 版本 2.1.6 的 Spring Boot 批处理应用程序。 我正在使用 JPA hibernate 进行数据库事务。

在我的 tasklet 中,我有类似的东西,

List<Documents> documents = documentReposirory.findAll();

for(Documents doc : documents){
   //do something with doc
   documentReposirory.updateDocument(doc);
}
//business logic
RepeatStatus.FINISHED 

我正在更新 for 循环中的 Document 表。但是只有在控件离开 tasklet 之后才会提交事务。

我的问题是,

1) 使用 jpa 持久化,是否可以在 update() 调用后立即手动提交事务?

2) 如果我正在更新Documents对象,即使我没有调用更新操作,系统会在控件离开tasklet后自动更新db..为什么会这样?我们如何防止这种情况发生?

我的存储库类是这样的,

public interface DocumentsRepository extends JpaRepository<Documents, Long> {

@Query("update .....")
public void updateDocument();
}

【问题讨论】:

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


    【解决方案1】:

    在使用 TransactionAttributeType.REQUIRED 和更新方法 TransactionAttributeType.MANDATORY 调用 Update 的方法上使用事务属性。

    更多文档Link Oracle EJB 3.0

         @TransactionAttribute(TransactionAttributeType.REQUIRED)
            public ... update (){
        ...
        List<Documents> documents = documentReposirory.findAll();
    
        for(Documents doc : documents){
           //do something with doc
           documentReposirory.updateDocument(doc);
        }
    ...
        }
        @TransactionAttribute(TransactionAttributeType.MANDATORY)
        public void updateDocument(Document doc){
        ...
        }
    

    【讨论】:

    • 但是我没有使用ejb。我正在使用休眠+弹簧启动
    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2015-07-24
    • 2014-04-16
    相关资源
    最近更新 更多