简体   繁体   中英

how to make auto commit a jpa transaction

I have a spring boot- batch application with spring boot version 2.1.6. I am using JPA hibernate for DB transactions.

In my tasklet, I have something like,

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

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

I am updating the Document table in the for loop. But the transaction is committed only after the control leaves the tasklet.

My question is,

1) With jpa persistence, is it possible to manually commit the transaction immediately after the update() call?

2) If i am updating the Documents object, even if i am not calling an update operation, the system automatically updates the db after the control leaves the tasklet.. Why is it so? How can we prevent this?

My Repository class is like,

public interface DocumentsRepository extends JpaRepository<Documents, Long> {

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

Use the transaction attributes over the method which calls the Update With the TransactionAttributeType.REQUIRED and the Update Method TransactionAttributeType.MANDATORY.

More documentation 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){
    ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM