5. Cache Management
Prev | Chapter 8. EntityManager | Next

## 5. Cache Management

    public void flush();

The `flush` method writes any changes that have been made in the current transaction to the datastore. If the `EntityManager` does not already have a connection to the datastore, it obtains one for the flush and retains it for the duration of the transaction. Any exceptions during flush cause the transaction to be marked for rollback. See Chapter 9, Transaction .

Flushing requires an active transaction. If there isn't a transaction in progress, the `flush` method throws a `TransactionRequiredException`.

    public FlushModeType getFlushMode();
    public void setFlushMode(FlushModeType flushMode);

The `EntityManager`'s `FlushMode` property controls whether to flush transactional changes before executing queries. This allows the query results to take into account changes you have made during the current transaction. Available `jakarta.persistence.FlushModeType` constants are:

- `COMMIT`: Only flush when committing, or when told to do so through the `flush` method. Query results may not take into account changes made in the current transaction.

- `AUTO`: The implementation is permitted to flush before queries to ensure that the results reflect the most recent object state.

You can also set the flush mode on individual `Query` instances.

### Note

OpenJPA only flushes before a query if the query might be affected by data changed in the current transaction. Additionally, OpenJPA allows fine-grained control over flushing behavior. See the Reference Guide's Section 8, “ Configuring the Use of JDBC Connections ”.

    public void clear();

Clearing the `EntityManager` effectively ends the persistence context. All entities managed by the `EntityManager` become detached.

Prev | Up | Next
4. Entity Identity Management | Home | 6. Query Factory