Data access and transactions

WebSphere® eXtreme Scale Client uses transactions. After an application has a connection to a data grid, you can access and interact with data in the data grid.

[Java programming language only]

Transactions in Java applications

With Java applications, you can establish a client connection to a distributed instance .

When an application interacts with a Session, it must be in the context of a transaction. A transaction is begun and committed or rolled back using the Session.begin, Session.commit, and Session.rollback methods on the Session object. Applications can also work in auto-commit mode, where the Session automatically begins and commits a transaction whenever the application interacts with Maps. However, the auto-commit mode is slower.

A thread in a Java application needs its own Session. When you want your application to use the ObjectGrid on a thread, call one of the getSession methods to obtain a Session. After the application is finished with the Session, call the Session.close() method. This method closes the session, returning it to the pool and releasing its resources. Closing a session is optional, but improves the performance of subsequent calls to the getSession() method. If the application is using a dependency injection framework such as Spring, you can inject a Session into an application bean when necessary.

After you obtain a Session, the application can access data stored in maps in the ObjectGrid. The map-based API is obtained by using the Session.getMap method.

[.NET programming language only]

Transactions in .NET applications

In .NET applications, each thread must have a separate IGridMapPessimisticTx or IGridMapPessimisticAutoTx object. With the IGridMapPessimisticTx object, you use the Transaction property to explicitly begin, commit or rollback the transaction. With the IGridMapPessimisticAutoTx object, the transaction begin, commit and rollback operations occur automatically. After you obtain one of these objects, the application can access stored data in the data grid.

The logic of using transactions

Transactions may seem to be slow. You must use transactions for the following reasons:
  1. To allow rollback of changes if an exception occurs or business logic needs to undo state changes.
  2. To hold locks on data and release locks within the lifetime of a transaction, allowing a set of changes to be made atomically, that is, all changes or no changes to data.
  3. To produce an atomic unit of replication.
  4. [Java programming language only] To update multiple partitions.

You can customize how much transaction support is needed. Your application can turn off rollback support and locking but at a cost to the application. The application must handle the lack of these features. Examples of how the application can manage transaction support follow:

[Java programming language only] If the application modifies an object retrieved using a NONE CopyMode value, it is changing the committed copy of that object directly. Rolling back the transaction has no meaning in this mode. You are changing the only copy in the ObjectGrid. Although using the NONE CopyMode is fast, be aware of its consequences. An application that uses a NONE CopyMode must never roll back the transaction. If the application rolls back the transaction, the indexes are not updated with the changes and the changes are not replicated if replication is turned on.

The default values are easy to use and less prone to errors. If you start trading performance in exchange for less reliable data, the application needs to be aware of what it is doing to avoid unintended problems.

Transactions and partitions

Transactions in Java applications can update a single or multiple partitions, however updating a single partition is the default behavior. .NET applications can only update a single partition.

Use the TxCommitProtocol Session API to enable multi-partition transaction support for WebSphere eXtreme Scale Client. You can use the following two options:
  • TxCommitProtocol.ONEPHASE (default): Transactions from a client can read from multiple partitions, but can update one partition only. Attempts made to update multiple partitions fail.
  • TxCommitProtocol.TWOPHASE: Transaction from a client can read and update multiple partitions. The transaction uses the two-phase commit protocol to ensure data written to the partitions are automatically committed or rolled back. If the transaction only writes to a single partition then a one-phase commitment protocol is used.