Reputation: 53
I am reading hibernate in action and came across the following
Persistent instances participate in transactions—their state is synchronized with the database at the end of the transaction. When a transaction commits, state held in memory is propagated to the database by the execution of SQL INSERT, UPDATE, and DELETE statements. This procedure might also occur at other times. For example, Hibernate might synchronize with the database before execution of a query. This ensures that queries will be aware of changes made earlier during the transaction.
I fail to understand the line in bold. How can when would such a situation arise. How would the synchronization happen.
Upvotes: 0
Views: 563
Reputation: 53
FlushMode.AUTO Flushing the Session state to the database at the end of a database transaction is required in order to make the changes durable and is the common case. Hibernate doesn’t flush before every query. However, if there are changes held in memory that would affect the results of the query, Hibernate will, by default, synchronize first.
FlushMode.COMMIT Specifies that the session won’t be flushed before query execution (it will be flushed only at the end of the database transaction). Be
FlushMode.NEVER Lets you specify that only explicit calls to flush() result in synchronization of session state with the database.
Reference: Spring In Action
Upvotes: 0
Reputation: 32831
Yes, there are two operations involved: updating the database to reflect the changes done to persistent objets; this operation is called flush. Hibernate may call it at any time during a transaction, then, the commit itself, which requires a flush to make sure the database is up to date, and a COMMIT in the underlying database which terminates the transaction.
Upvotes: 0
Reputation: 422
The synchronization actually saves the previous changes made on database. To do that, you can use the flush() method.
Usually you do that before another sql query in order to make sure the changes have been made.
Upvotes: 1