8.4. Persistence-Capable Lifecycle Management
Prev | Chapter 8. PersistenceManager | Next

## 8.4. Persistence-Capable Lifecycle Management

`PersistenceManager`s perform several actions that affect the lifecycle state of persistence-capable instances. Each of these actions is represented by multiple methods in the `PersistenceManager` interface: one method that acts on a single persistence-capable object, such as `makePersistent`, and corresponding methods that accept a collection or array of persistence-capable objects, such as `makePersistentAll`.

    public void makePersistent (Object pc);
    public void makePersistentAll (Collection pcs);
    public void makePersistentAll (Object[] pcs);

Transitions transient instances to persistent-new. This action can only be used in the context of an active transaction. When the transaction is committed, the newly persisted instances will be inserted into the data store.

    public void deletePersistent (Object pc);
    public void deletePersistentAll (Collection pcs);
    public void deletePersistentAll (Object[] pcs);

Transitions persistent instances to persistent-deleted, or persistent-new instances to persistent-new-deleted. This action, too, can only be called during an active transaction. The given instance(s) will be deleted from the data store when the transaction is committed.

    public void makeTransient (Object pc);
    public void makeTransientAll (Collection pcs);
    public void makeTransientAll (Object[] pcs);

This action transitions persistent instances to transient. The instances immediately lose their association with the `PersistenceManager` and their JDO identity. The data store records for the instances are not modified.

This action can only be run on clean objects. If it is run on a dirty object, a `JDOUserException` is thrown.

    public void makeTransactional (Object pc);
    public void makeTransactionalAll (Collection pcs);
    public void makeTransactionalAll (Object[] pcs);

Use this action to make transient instances transient-transactional, or to bring persistent-nontransactional instances into the current transaction. In the latter case, the action must be invoked during an active transaction.

    public void makeNontransactional (Object pc);
    public void makeNontransactionalAll (Collection pcs);
    public void makeNontransactionalAll (Object[] pcs);

Transitions transient-clean instances to transient, and persistent-clean instances to persistent-nontransactional. Invoking this action on a dirty instance will result in a `JDOUserException`.

    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. 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 refresh (Object pc);
    public void refreshAll (Collection pcs);
    public void refreshAll (Object[] pcs);
    public void refreshAll ();

Use the `refresh` action to make sure the persistent state of an instance is in synch with the values in the data store. `refresh` is intended for long-running optimistic transactions in which there is a danger of seeing stale data.

Calling the `refreshAll` method with no parameters acts on all transactional objects in the cache. If there is no transaction in progress, the method is a no-op.

    public void retrieve (Object pc);
    public void retrieveAll (Collection pcs);
    public void retrieveAll (Object[] pcs);
    public void retrieveAll (Collection pcs, boolean dfgOnly);
    public void retrieveAll (Object[] pcs, boolean dfgOnly);

Retrieving a persistent object immediately loads all of the object's persistent fields with their data store values. You might use this action to make sure an instance's fields are fully loaded before transitioning it to transient. Note, however, that this action is not recursive. That is, if object A has a relation to object B, then passing A to `retrieve` will load B, but will not necessarily fill B's fields with their data store values.

Prev | Up | Next
8.3. Transaction Association | Home | 8.5. Lifecycle Examples