Random Post: Rolling the DICE
RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    A Brief Look at Local Transactions in GigaSpaces

    Introduction

    One way to think about GigaSpaces1 is as a sort of database management system for maintaining and accessing data spread across a set of caches. Of course this view ignores many important capabilities of the GigaSpaces framework, but it is a useful perspective for considering GigaSpaces’ transactional features.

    GigaSpaces offers transactional control over access to data in its spaces. When the right combinations of API features are employed, data operations assume ACID characteristics.

    As a reminder, ACID is an acronym standing for “atomic, consistent, isolated and durable”. The term refers to the behaviour that is generally expected from transactional systems. Quoting Wikipedia:

    • Atomicity: Either all the tasks in a transaction must be done, or none of them. The transaction must be completed, or else it must be undone (rolled back).

    • Consistency: Every transaction must preserve the integrity constraints — the declared consistency rules — of the database. It cannot place the data in a contradictory state.

    • Isolation: Two simultaneous transactions cannot interfere with one another. Intermediate results within a transaction are not visible to other transactions.

    • Durability: Completed transactions cannot be aborted later or their results discarded. They must persist through (for instance) restarts of the DBMS after crashes

    It is important to understand that GigaSpaces transactions govern only the state of data in the spaces managed by GigaSpaces. Unlike the transactional support provided by heap-oriented products such as Terracotta and Kabira, GigaSpaces transactions do not provide guarantees concerning access to or the state of heap memory in a GigaSpaces application.

    Lineage

    GigaSpaces’ transaction support derives from three sources:

    1. Jini – At its core, GigaSpaces is an implementation of the JavaSpaces specification, which is a component of the Jini specification. Jini was designed to allow heterogeneous software and hardware devices to interact. The Jini specification (and reference implementation) include a facility for Jini-compliant devices to participate in distributed transactions. GigaSpaces has inherited and extended this capability.

    2. JTA (Java Transaction API) – Jini provides the ability to orchestrate transactions among Jini-compliant participants such as JavaSpaces. Sometimes, however, it may be necessary to engage in a transaction both participants that are Jini-compliant and participants that do are not Jini-compliant. For example, a GigaSpaces application might need to remove a data entry from a space and insert a corresponding row into a table in an RDBMS. Most RDBMSs support a distributed transaction protocol called XA that allows them to participate in transactions with otherwise independent participants. Using JTA, GigaSpaces can participate in XA distributed transactions.

    3. Spring – The Spring framework provides an abstraction of transaction management services and constructs. GigaSpaces has embraced this abstraction and uses it as the façade for its own transaction support.

    To this mix GigaSpaces adds support for local transactions, meaning transactions that involve only one instance of one space.

    Transaction Control

    Mirroring Spring’s capabilities, GigaSpaces offers two modes of transaction control, programmatic and declarative. With programmatic transaction control, the programmer uses API calls to configure, start, commit and abort transactions. With declarative transaction control, the programmer includes directives about where and how transactional behaviour should be applied. These directives are interpreted by Spring and translated into transactional control statements that are woven into the application at runtime.

    Spring declarative transaction control itself takes two forms, both of which are supported by GigaSpaces. First, it can be configured using a pointcut specification typical of aspect-oriented implementations. Second, methods can be annotated to indicate that they should (or should not) be executed within transactions.

    A Simple Example

    Get the source code for this example here.

    Here is a simple application that illustrates how to implement a local transaction with GigaSpaces using annotation-driven declarative transaction control. The example creates two instances of a class, then writes both instances to a space within a transaction.

    There are five files:

    1. A spring application context file – GSSimpleTranExample.xml – that sets up the proxy by which the application will access the space. The transaction manager is defined here.

    2. A simple POJO class – TestClass.java – two instances of which will be written to the space under a transaction.

    3. A Java interface – ConnBeanInterface.java – that declares the methods that will be used to access the space. The interface – implementation pattern is used because Spring works better with instances of interfaces than with instances of concrete classes.

    4. A Java bean – ConnBean.java – that performs the space operations.

    5. A Java main program – GSSimpleTranExample.java – that instantiates the objects to be written to the space and invokes the method to write them.

    Let’s start by looking at the Spring application context file. The first item of interest is an Spring namespace element that instructs Spring to apply transactional controls to methods that are annotated with @Transactional in beans that it is managing:

    <tx:annotation-driven />

    The name of a transaction manager bean can be specified as an attribute to this element. This is the bean containing the transaction manager that will be used to manage the transactional behaviour of the annotated methods that Spring finds in the beans that it manages. If none is specified, as in our example, a default value of “transactionManager” is assumed.

    Next is an OpenSpaces namespace element that instruct Spring to instantiate a transaction manager:

    <os-core:local-tx-manager id=“transactionManager” space=“gSSimpleTranExample”/>

    In our case we are using GigaSpaces’ local transaction manager. This is the best choice when each transaction will involve only a single partition of a single space.

    Notice that the transaction manager declaration contains a reference to a space. As we will see shortly, this construct is one of two that constitute an apparent redundancy in the OpenSpaces namespace support for transactions.

    Next we see a typical OpenSpaces namespace space declaration that tells Spring to create a an IJSpace instance:

    <os-core:space id=“gSSimpleTranExample” url=“jini://*/*/GSSimpleTranExample” />

    There is nothing specifically transactional about it; it is included in this discussion because the next element, which has a transactional dimension, refers to it.

    Next is an OpenSpaces declaration of a GigaSpace:

    <os-core:giga-space id=“gigaSpace” space=“gSSimpleTranExample”

    tx-manager=“transactionManager” />

    Note that this element includes both an explicit reference to the transaction manager declared earlier, and refers to the space that was defined earlier and that also refers to the transaction manager.

    <os-core:giga-space id=“gigaSpace” space=“gSSimpleTranExample”

    tx-manager=“transactionManager” />

    Next we specify our application bean that will perform the space operations:

    <bean id=“connBean” class=“ConnBean” />

    Spring will instantiate this bean and manage its lifecycle.

    We also include the OpenSpaces GigaSpace context element:

    <os-core:giga-space-context />

    so that Spring will assign the GigaSpace bean declared in the context file to a variable of type GigaSpace that is annotated with the @GigaSpaceContext annotation in our ConnBean instance.

    The TestClass is not transaction-aware, and is not described further.

    The ConnBeanInterface declares the method that will be implemented in ConnBean:

    public interface ConnBeanInterface {

    public void writeTwoObjects(TestClass tCI1, TestClass tCI2);

    }

    Note that, although it is not transaction-aware, it could have been, as we have the option of annotating the interface class or its methods to be transactional.

    The implementation of:

    public void writeTwoObjects(TestClass tCI1, TestClass tCI2) {

    in our ConnBean class is transactional:

    @Transactional

    public void writeTwoObjects(TestClass tCI1, TestClass tCI2) {

    gigaSpace.write(tCI1);

    gigaSpace.write(tCI2);

    }

    The @Transactional annotation tells Spring to wrap transactional controls around this method.

    From our main application class, here is the code that creates two instances of TestClass, then invokes the ConnBean method that will write them to the space under a transaction:

    TestClass tCI1 = new TestClass(0);

    TestClass tCI2 = new TestClass(1);

    connBean.writeTwoObjects(tCI1, tCI2);

    Running the Example

    Before running this example, create an unpartitioned space called GSSimpleTranExample.

    When you run the example pass the path and name of the spring application context file as a command line argument.

    It it runs successfully the program will produce the following output:

    Done with my work. About to exit.

    Proving the Transactional Behaviour

    Here are a few techniques that can be used to explore the transactional behaviour in this example:

    1. Extend the duration of the transaction and inspect it in the GigaSpaces GUI Space Browser while it is in progress. You can extend the duration of the transaction by modifying the ConnBean class as follows:



      int sleepLength = 10000;

    @Transactional

    public void writeTwoObjects(TestClass tCI1, TestClass tCI2) {

    gigaSpace.write(tCI1);

    gigaSpace.write(tCI2);

    try {

    Thread.sleep(sleepLength);

    } catch (Exception e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    sleepLength is specified in milliseconds. Set it to whatever value is convenient for you. Then run the application, and look at the list of transactions. Note that the transaction type is “Local” because we declared a local transaction manager in the application context file. Also notice that two objects are locked by this transaction. These are the two objects that are being inserted.

    2. Try to query the locked objects using the Space Browser and observe that they cannot be read while the transaction is in progress. To do this, start by removing any instances of TestClass from the space. Set the value of sleepLength to a long enough duration (perhaps 30 seconds) that you will have time to execute a query against the space while the transaction is in progress. Run the program. Then select the TestClass class in the space browser and execute a query. The result set will be empty.

    As you are preparing to run the query you will notice that the instance count for the TestClass class is two, not zero. This is because the method that is used by the GUI to inspect the space has access to locked objects and includes them in the count value it returns. The objects themselves, however, are not visible until the transaction commits.

    3. Force the transaction to roll back, and observe that the space is left empty. To force a roll-back, raise an exception in the WriteTwoObjects() method as follows:

      int sleepLength = 10000;

    @Transactional

    public void writeTwoObjects(TestClass tCI1, TestClass tCI2) {

    gigaSpace.write(tCI1);

    gigaSpace.write(tCI2);

    try {

    Thread.sleep(sleepLength);

    } catch (Exception e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    throw (new RuntimeException());

    }

    Again, start by removing any instances of TestClass from the space. Now when you run the application you see the transaction in progress and the TestClass instance count will go to two. At the end of the period specified by sleepLength, the transaction will abort and the instance count will revert to zero.

    1GigaSpaces technology is spread across two code bases, GigaSpaces and OpenSpaces. This paper often refers to all of the technology indiscriminately as “GigaSpaces”.

    6 Responses to “A Brief Look at Local Transactions in GigaSpaces”

    1. Gary Berger says:

      Love to talk to you about your observations..

      -g

    2. subuta says:

      That would be my pleasure. I will contact you by email.

      -Dan

    3. Joker says:

      Amazing! Not clear for me, how offen you updating your blog.scapps.co.uk.
      Joker

    4. subuta says:

      Hi Joker,

      Thanks for your note. I suggest that, if you are interested in knowing when there is an update, you subscribe to the RSS feed. It seems to work well.

      -Dan

    5. subuta says:

      Hello,

      I regret that I am unable to understand your comment.

      -Dan

    6. [...] I posted a quick example and explanation of a GigaSpaces local transaction.  You can find the post here and get the code [...]

    Leave a Reply