Samuel Gebretnsae
Samuel Gebretnsae

Reputation: 43

Does JPA and Hibernate automatically update database without transaction begin and commit

In JPA, if I try to update a managed object using setter and getter method without using transaction begin and commit, does JPA automatically update the database ( not immediately but later) considering FlushType is in AUTO.

Upvotes: 3

Views: 8262

Answers (2)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153780

In JPA, the entity state transitions are not automatically synchronized with the database. If the entity is attached to the persistence context, then at flush-time, the automatic dirty checking mechanism translates object state changes into DML statements.

But that requires the entity to be managed by the persistence context, as otherwise, the dirty checking mechanism will not trigger.

Although the JPA specification requires only entity state transitions to be wrapped in a logical transaction:

You should ALWAYS use transaction, even when only reading data.

If you don't explicitly use transactions when reading data, then you fall back to auto-commit mode, putting additional pressure on the connection pooling mechanism, and ending up with one database transaction per query.

Upvotes: 7

Neil Stockton
Neil Stockton

Reputation: 11531

The implementation I use (DataNucleus JPA) does allow this (like an auto-commit mode). If that mode is not enabled then it falls back to what standard JPA does which is to hold on to those changes until the next transaction.

Upvotes: 0

Related Questions