The API for WebSphere® eXtreme Scale
Client requires each thread to have a separate IGridMapPessimisticTx
or IGridMapPessimisticAutoTx object. With the IGridMapPessimisticTx
object, the Transaction property is used to explicitly begin, commit
or roll back the transaction. With the IGridMapPessimisticAutoTx object,
the transaction begin, commit and rollback operations occur automatically.
Use sessions to interact with data, including Add, Put, and Replace
operations.
About this task
The IGridMapPessimisticTx and IGridMapPessimisticAutoTx interfaces provide operations such as Add, Get, Put, Replace, and Remove to manipulate the data. The IGridMapPessimisticTx interface provides additional operations
such as Lock and GetAndLock to
control concurrent access to the data.
Procedure
- Add data.
The following code fragment demonstrates
how to use the IGridMapPessimisticTx interface
to begin a new transaction, create an item for the data grid, and
then commit the entire transaction.
IGridMapPessimisticTx<String,Person> ptmap;
ptmap = grid.GetGridMapPessimisticTx<String,Person>("PERSON");
ptmap.Transaction.Begin();
Person p = new Person();
p.name = "John Doe";
ptmap.Add(p.name, p);
ptmap.Transaction.Commit();
The
pattern is designed to obtain references to the maps for the thread,
start a transaction, work with the data, then commit the transaction.
- Replace data.
The following code fragment
demonstrates how to use the IGridMapPessimisticTx interface to begin a new transaction, lock an item in the data grid
and obtain its value, replace the item value, and commit the transaction.
IGridMapPessimisticTx<String,Person> ptmap;
ptmap = grid.GetGridMapPessimisticTx<String,Person>("PERSON");
ptmap.Transaction.Begin();
Person p = ptmap.GetAndLock("John Doe", LockMode.Upgradable);
p.age = 30;
ptmap.Replace(p.name, p);
ptmap.Transaction.Commit();
The application
normally uses the GetAndLock method rather than
a simple get to lock the record. The method must be called to provide
the updated value to the map. If the Replace method
is not called, then the map is not changed.