There are five 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:
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.
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.
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.
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.
Results
A series of scenarios are provided to help you understand:
▪ How to create context and change data values when users log on.
▪ How to fetch and change data when users perform transactions.
▪ How to fetch data and destroy context when users log off.
The following scenarios focus on context creating and data value changing. These scenarios happen in scenes like establishing a session when a user logon, and perform the first step of a transaction.
Scenario I (for Persistence Shared CHA or Memory Shared CHA)
Given that you already have a root Context A:
1 Create a remote Context B
2 Chain the newly created Context B to the root Context A
3 Change the value of Context B using the APIs like setValueAt(), setKeyedCollection()
4 Commit the Context B
Scenario II (for Persistence Shared CHA or Memory Shared CHA)
Given that you already have a root Context A:
1 Crate a local Context B
2 Chain the newly created Context B to Context A
3 Change the value of Context B using the APIs like setValueAt(), setKeyedCollection()
4 Get the value of Context B using the APIs like getValueAt(), getKeyedCollection()
5 Unchain the Context B
Scenario III (for Persistence Shared CHA or Memory Shared CHA)
Given that you already have a remote Context A:
1 Create a remote Context B
2 Chain the newly created Context B to the Context A
3 Change the value of Context B using the APIs like setValueAt(), setKeyedCollection()
4 Commit the Context A
Scenario IV
Given that you have a remote Context A:
1 Create a local Context B
2 Chain the Context B to the Context A
3 Change the value of Context B using the APIs like setValueAt(), setKeyedCollection()
4 Get the value of Context B using the APIs like getValueAt(), getKeyedCollection()
5 Unchain the Context B
6 Commit the Context A
Scenario V (for Persistence Shared CHA, Memory Shared CHA, or Local CHA)
Given that you already have a local Context A:
1 Create a local Context B
2 Chain Context B to Context A
3 Change the value of Context B using the APIs like setValueAt(), setKeyedCollection()
4 Get the value of Context B using APIs like getValue(0, getKeyedCollection()
The following scenarios focus on Data Fetching and Context destroying. These scenarios happen when user logs off, which is the last step of the transaction.
Scenario X (for Persistence Shared CHA or Memory Shared CHA)
1 Use the API getContextbyInstanceID() to get the Context A with the given Instance ID
2 Get the value of the Context A using the APIs like getValueAt(), getKeyedCollection()
3 Unchain the Context A from the parent Context
4 Prune the Context A;
Scenario XI (for Persistence Shared CHA or Memory Shared CHA)
1 Use the API getContextbyInstanceID() to get the Context A with the given Instance ID
2 Get Context B, one of the Children of the Context A
3 Get the value of the Context B using the APIs like getValueAt(), getKeyedCollection()
4 Unchain the Context B from the parent Context
5 Prune the Context B
Scenario XII (for Local CHA)
1 Use the API getContextbyInstanceID() to get the Context A with the given Instance ID
2 Get the value of the Context A using the APIs like getValueAt(), getKeyedCollection()
3 Unchain the Context A from the parent Context
Scenario XIII (for Local CHA)
1 Use the API getContextbyInstanceID() to get the Context A with the given Instance ID
2 Get Context B, one of the Children of the Context A
3 Get the value of the Context B using the APIs like getValueAt(), getKeyedCollection()