Context life cycle

About this task

There 5 stages in context's life cycle: uncreated, unchained-uncommitted, chained-uncommitted, chained committed, unchained-committed.

These stages can transfer from one to another as shown in the following figure:

CHA lifecycle

Procedure

  1. Create a Context (from unchained to unchained-uncommitted). APIs such as ContextFactory.createContext() are used to create a specific context. In most cases, the setting of a context is read from the external file. In the constructing method, it has several parameters to indicate the type, mode, parent, and kColl of this context.
  2. Chain a Context (from unchained-uncommitted to chained-uncommitted). In CHA, contexts are organized as a tree. There is always a root context, and every newly created context has a parent context to chain to. Therefore, you can navigate and search a certain context from the root context in the context tree. For example, aContext.chainTo(bContext) means that aContext is chained to bContext as a child.
    Note: A local or remote context can be chained to a remote context; a local context can be chained to a local context; but a remote context cannot be chained to a local context. When a local context is chained to a local context, or a remote context is chained to a remote context, both the parent and child will record their relationship, but when a local context is chained to a remote context, only the child will record their relationship.

    Diagram showing how to chain a context

  3. Search a Context. You can use navigation and searching methods in CHA to navigate a Context tree or search a certain context, such as context.getContextByInstanceID(), context.getContextNamed(), context.getNextContextVertical(). With these methods, it is easy to locate a context in the context tree.
  4. Fetch and change data in the Context. The data in the context is stored in KeyedCollection, which is a recursively defined data structure. CHA provides plenty of methods to access the data. They are data fetching methods like context.getKeyedCollection(), context.getValueAt(), context.getElementAt(); and data changing methods like context.setKeyedCollection(), context.setValueAt().
    Note: Sometimes, the fetching and changing operation not only affects the current invoking context, but also the up-level parent or ancestor contexts.
  5. Commit a Context (from chained-committed to chained-committed). To commit a context is to store the context to persistence. It is used to finish a single transaction. In CHA, data related to a single transaction is stored in several contexts; and these contexts are organized into a branch of the CHA context tree. To commit the transaction data when the transaction is finished will always store the whole branch into persistence. When the committing of a context is invoked, CHA will store this context and its offspring into persistence. Context.commit() is provided to commit a context.
    Note: Only remote context can be committed.

    Diagram illustrating how to commit a context

  6. Unchain a Context (from chained-committed to unchained-committed). Context.unchain() is provided to break the link between a context and its parent context.

    Diagram showing how to unchain a context

  7. Prune a Context (from unchained-committed to uncreated). When a context is not to be used any more, it should be removed from both memory and persistence. To local context, CHA only unchain it from the parent context (leave ti to JVM GC to cleanup). To remote context, CHA will remove this context from both memory cache and persistence.
    Note: Similar to committing a context, pruning affects the whole branch under the current invoking context.

    Diagram showing the pruning of a context.

Results

A series of scenarios are provided to help you understand: