Calling flush() in @Transactional method in Spring Boot application

Cybex :

Is it possible that calling Hibernate flush() in the middle of @Transactional method will save incomplete results in the database? For example, is it possible that this function could save "John" to the database?

@Transactional
public void tt() {
    Student s = new Student("John");
    em.persist(s);
    em.flush();
    // Perform some calculations that change some attributes of the instance
    s.setName("Jeff");
}

I tried it with H2 in-memory database and it didn't save incomplete transaction changes. But is it possible under certain conditions and maybe with another DB engine?

pirho :

It should not save anything before you call em.commit() or transaction ends. The best explanation I found is from here .Below the essential excerpt:

This operation will cause DML statements (insert/update/delete etc) to be executed to the database but the current transaction will not be committed. That means flush() will not make current changes visible to other EntityManager instances or other external database clients; that will only happen at the transaction commit. In other words flush() operation will only flush the current memory cache from EntityManager to the database session.

So the flush might raise some JPA exceptions but it would not actually be committed to database before transaction ends.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91369&siteId=1