Cool GridGain Transaction Example

DZone 's Guide to

Cool GridGain Transaction Example

· Cloud Zone ·
Free Resource

I never planned to create an example like this, but one of the users in our community forums pushed me to do it. Basically the user was complaining that he gets some deadlock behavior on the grid when he reads and updates the same object from cache on multiple grid nodes in parallel. Naturally I got worried and created the following test, which passes by the way :)

In this example we basically store array of size N in cache and execute N jobs on the grid, each of which gets an array from cache and flips the array cell assigned to it from zero to one. As a result you start out with an array of all zeros and you end up with array of all ones. The cache is configured in PARTITIONED mode with synchronous commits. By default, every key stored in the cache is backed up on another node. Here is how the code looks like:

// Number of grids in the test.
private static final int GRID_COUNT = 4;

public void testTransactions() throws GridException {
// Start multiple grid nodes in the same JVM.
for (int i = 0; i < GRID_COUNT; i++) {
GridConfigurationAdapter cfg = new GridConfigurationAdapter();

// I am specifically omitting other configuration details.
cfg.setGridName("grid-" + i);


try {
// Try job count N = 1000
finally {

private void checkTransactions(final int jobCnt) throws GridException {
// Get frist grid node.
Grid grid = G.grid("grid-0");

final GridCache<String, int[]> cache = grid.cache();

Collection<GridFuture<?>> futs = new LinkedList<GridFuture<?>>();

for (int i = 0; i < jobCnt; i++) {
// Run these jobs in parallel on all grid nodes.
GridFuture<?> fut = grid.runAsync(
new GridInClosureX<Integer>() {
public void applyx(Integer i) throws GridException {
GridCacheTx tx =

try {
int[] arr = cache.get("TestKey");

if (arr == null)
arr = new int[jobCnt];

arr[i] = 1;

cache.put("TestKey", arr);

finally {


for (GridFuture<?> fut : futs)
fut.get(); // Wait for completion.

// Get array from cache.
int[] arr = cache.get("TestKey");

assert arr != null;
assert jobCnt == arr.length;

// Make sure that all array cells got flipped to 1.
for (int i : arr)
assert i == 1;

By the way, we have recently released GridGain 3.1.0, so give it a try.

From http://gridgain.blogspot.com/2011/05/cool-gridgain-transaction-example.html


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}