8.9. Cache Management
Prev | Chapter 8. PersistenceManager | Next

## 8.9. Cache Management

These methods allow you to interact indirectly with the `PersistenceManager`'s cache.

    public void evict (Object pc);
    public void evictAll (Collection pcs);
    public void evictAll (Object[] pcs);
    public void evictAll ();

Evicting an object tells the `PersistenceManager` that your application no longer needs that object. Unless it is dirty, the object transitions to hollow and the `PersistenceManager` releases all strong references to it, allowing it to be garbage collected.

Calling the `evictAll` method with no parameters acts on all persistent-clean objects in the `PersistenceManager`'s cache.

    public void flush ();

The `flush` method writes any changes that have been made in the current transaction to the datastore. If the `PersistenceManager` 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, as if `Transaction.setRollbackOnly` were called. See Chapter 9, Transaction.

Flushing has no effect outside of a transaction.

    public void checkConsistency ();

This method validates the dirty objects in the `PersistenceManager` cache against the datastore. Within a datastore transaction, this method may delegate to `flush`. Within an optimistic transaction, this method obtains a datastore connection and checks that the instances in cache are consistent with their datastore records. If the implementation detects any inconsistencies, such as concurrent modification by another thread/process, it throws a `JDOOptimisticVerificationException`. Any datastore resources obtained during the course of this method are released before the method returns. This method has no effect outside of a transaction.

You can use `checkConsistency` to determine if a subsequent commit is likely to succeed. If `checkConsistency` throws a `JDOOptimisticVerificationException`, you have an opportunity to gather the failed objects in the exception and its nested exceptions and fix their inconsistencies before committing.

Prev | Up | Next
8.8. JDO Identity Management | Home | 8.10. Extent Factory