Over a million developers have joined DZone.

Cool GridGain Transaction Example

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

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

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}