Summary: OpenSpaces provides several implementations of Spring's PlatformTransactionManager allowing you to use the GigaSpaces and Jini Transaction Manager.

Overview

Spring Framework provides a transaction manager abstraction using the PlatformTransactionManager interface with several different built-in implementations, such as JDBC Data Source and JTA. OpenSpaces provides several implementations of Spring's PlatformTransactionManager, allowing you to use the GigaSpaces and Jini Transaction Manager.

By implementing Spring's PlatformTransactionManager, OpenSpaces users can utilize Spring's rich support for declarative transaction management. Declarative transaction management can be easily utilized using the GigaSpace simplified space API.

In order to have GigaSpace transactional, the transaction manager must be provided as a reference when constructing the GigaSpace bean.

OpenSpaces supports several transaction managers, and changing a Transaction Manager to work with is just a matter of changing the Transaction Manager implementation.

Time based Parameters Units:
  • The default-timeout paramter is in second units.
  • The commit and abort timeout , lookup-timeout , duration and round-trip-time parameters are in millisecond units.

Local Jini Transaction Manager

GigaSpaces' built-in, high-performance, single space Local Transaction Manager can be easily defined using OpenSpaces (and then utilized using GigaSpace).

The Local Transaction Manager only works on a single Space instance. When working with a clustered topology, and performing operations that span over several cluster members, the Jini Transaction Manager should be used. Note, this can be avoided when working with the SBA model, where each cluster member works only with its local Space instance.

Here is an example of how this can be defined in a Spring application context:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:local-tx-manager id="transactionManager" space="space"/>

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager"/>

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.LocalJiniTransactionManager">
	<property name="space" ref="space" />
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
	<property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
PlatformTransactionManager ptm = new LocalJiniTxManagerConfigurer(space).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

When defining a local transaction manager, the Space bean is used as a transactional context and an IJSpace provider (the GigaSpaces Local Transaction Manager requires it).

The LocalJiniTransactionManager is an implementation of the Spring PlatformTransactionManager, allowing Spring's transaction support to be used with OpenSpaces.

Timeout Values

The local transaction manager allows to set the default timeout value for transactions. A timeout value is used when a transaction is not committed/rolled back (for example due to JVM exit) to control when the transaction will be discarded. By default the timeout value is 60 seconds and is set in seconds. Controlling the timeout value can be done using:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:local-tx-manager id="transactionManager" space="space" default-timeout="1000"/>

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager"/>

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.LocalJiniTransactionManager">
	<property name="space" ref="space" />
	<property name="defaultTimeout" value="1000" />
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
	<property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
PlatformTransactionManager ptm = new LocalJiniTxManagerConfigurer(space).defaultTimeout(1000).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

When using Spring declarative transaction management, a transaction timeout can be set on the transaction scope (a method for example). This timeout setting will be used as the timeout value for the transaction (note that this timeout value is set in seconds - per Spring spec). If no timeout is set, the default timeout set on the transaction manager is used.

When using Jini based transactions, a timeout value can be set for both the commit and abort operations. This values can also be set on the transaction manager.

Distributed Jini Transaction Manager

The distributed Jini Transaction Manager starts an embedded distributed (mahalo) Jini Transaction Manager, which is then wrapped with an implementation of the Spring PlatformTransactionManager. This transaction manager is usually used in order to perform distributed transactions spanning multiple space instances.

Below is an example of how it can be defined in a Spring application context:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:distributed-tx-manager id="transactionManager" />

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager" />

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.DistributedJiniTransactionManager" />

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
    <property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
PlatformTransactionManager ptm = new DistributedJiniTxManagerConfigurer().transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

Timeout Values

The Jini distributed (mahalo) transaction manager allows to set the default timeout value for transactions. A timeout value is used when a transaction is not committed/rolled back (for example due to JVM exit) to control when the transaction will be discarded. By default the timeout value is 60 Sec and is set in seconds. Controlling the timeout value can be done using:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:distributed-tx-manager id="transactionManager" default-timeout="1000"/>

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager"/>

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.DistributedJiniTransactionManager">
	<property name="defaultTimeout" value="1000" />
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
	<property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
PlatformTransactionManager ptm = new DistributedJiniTxManagerConfigurer().defaultTimeout(1000).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

When using Spring declarative transaction management, a transaction timeout can be set on the transaction scope. For more details, see above.

When using Jini based transactions, a timeout value can be set for both the commit and abort operations. This values can also be set on the transaction manager.

Lookup Jini Transaction Manager

The lookup Jini Transaction Manager allows you to use the Jini lookup mechanism in order to lookup a Jini Transaction Manager, which is then wrapped with an implementation of the Spring PlatformTransactionManager. This transaction manager is usually used in order to obtain a remote Jini Mahalo transaction manager for distributed transactions spanning multiple spaces.

Below is an example of how it can be defined in a Spring application context:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:jini-tx-manager id="transactionManager" lookup-timeout="5000" />

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager" />

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.LookupJiniTransactionManager">
	<property name="lookupTimeout" value="5000" />
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
    <property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
PlatformTransactionManager ptm = new LookupJiniTxManagerConfigurer().lookupTimeout(5000).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

Timeout Values

The Jini lookup transaction manager allows to set the default timeout value for transactions. A timeout value is used when a transaction is not committed/rolled back (for example due to JVM exit) to control when the transaction will be discarded. By default the timeout value is 60 Sec and is set in seconds. Controlling the timeout value can be done using:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:jini-tx-manager id="transactionManager" default-timeout="1000"/>

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager"/>

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.LookupJiniTransactionManager">
	<property name="defaultTimeout" value="1000" />
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
	<property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
PlatformTransactionManager ptm = new LookupJiniTxManagerConfigurer().defaultTimeout(1000).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

When using Spring declarative transaction management, a transaction timeout can be set on the transaction scope. For more details, see above.

When using Jini based transactions, a timeout value can be set for both the commit and abort operations. This values can also be set on the transaction manager.

Renewing Transactions

Both Local and Jini transactions allow you to configure automatic renewing of ongoing transactions. This feature is very handy when wanting to configure a long transaction timeout, and have it expire earlier in case of a complete failure (for example, JVM crash). Expiring the transaction is important so Entries held under a transaction lock are released as soon as possible.

Here is an example of how this can be configured:

Namespace
<os-core:space id="space" url="/./space" />

<os-core:local-tx-manager id="transactionManager" space="space">
    <os-core:renew pool-size="2" duration="1000" round-trip-time="500" />
</os-core:local-tx-manager>

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager"/>

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="transactionManager" class="org.openspaces.core.transaction.manager.LocalJiniTransactionManager">
	<property name="space" ref="space" />
	<property name="leaseRenewalConfig">
	    <bean class="org.openspaces.core.transaction.manager.TransactionLeaseRenewalConfig">
	        <property name="poolSize" value="2" />
	        <proeprty name="renewRTT" value="500" />
	        <property name="renewDuration" value="1000" />
        </bean>
	</property>
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
	<property name="transactionManager" ref="transactionManager" />
</bean>

Code
IJSpace space = new UrlSpaceConfigurer("/./space").space();
TransactionLeaseRenewalConfig config = new TransactionLeaseRenewalConfig();
config.setPoolSize(2);
config.setRenewDuration(1000);
config.setRenewRTT(500);
PlatformTransactionManager ptm = new LocalJiniTxManagerConfigurer(space).leaseRenewalConfig(config).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace();

The above configuration creates a Local Transaction Manager with a pool of 2 transaction (lease) renewal managers (a single manager can handle multiple transactions, more managers allow for better concurrency). Each transaction is renewed every 1 second (1000 milliseconds) with an expected round trip time of 500 milliseconds. This means that a transaction with a timeout of 10 seconds is renewed 10 times (approximately) and if the JVM crashes, the transaction expires within a second (at most).

XA/JTA Support

GigaSpaces can be used within a XA transaction using JTA. OpenSpaces allows you to work with Spring's JTATransactionManager and provides support for declarative transaction management. Here is an example of how OpenSpaces JTA support can be used (using JOTM):

Namespace
<os-core:space id="space" url="/./space" />

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction" ref="jotm" />
</bean>

<os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager" />

Plain XML
<bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
    <property name="url" value="/./space" />
</bean>

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction" ref="jotm" />
</bean>

<bean id="gigaSpace" class="org.openspaces.core.GigaSpaceFactoryBean">
    <property name="space" ref="space" />
    <property name="transactionManager" ref="transactionManager" />
</bean>

Code
UserTransaction userTransaction = ... //get UserTransaction via JDNI / instantiation
PlatformTransactionManager ptm = new JtaTransactionManager(userTransaction);
IJSpace space = new UrlSpaceConfigurer("/./space").space();
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gig

GigaSpaces JTA support uses the high-performance Local Transaction Manager internally. The Local Transaction Manager can only be used when performing operations against a single Space instance. When working with a clustered topology using a proxy that connects to the whole cluster, operations should be performed only against a single cluster member (controlled using routing index). Working with two different Spaces (and not different cluster members) is supported with JTA.

XA transactions should generally be avoided and not used. The overhead of managing a 2PC transaction over two or more resources is often times a performance killer. We recommend using well-known patterns in order to avoid it (such as duplicate detection).

A Note about Programmatic Transaction Management

If you don't want to leverage Spring's declarative transaction management, or have an application that is not configured by Spring, you can start, commit and rollback transactions explicitly from within your code by using Spring's transaction API.
After getting a reference to the PlatformTransactionManager (see above under the "Code" tab of all the code samples), you should use the following code to manage transactions:

GigaSpace gigaSpace = ...//get reference to a GigaSpace instance
PlatformTransactionManager ptm = ... //get reference to a GigaSpaces PlatformTransactionManager instance
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
//configure the definition...
TransactionStatus status = ptm.getTransaction(definition);
try {    
    //do things with gigaSpace...
} 
catch (MyException e) {
    ptm.rollback(status);
    throw e;
}
ptm.commit(status);

You can also use Spring's TransactionTemplate if you prefer. This is documented in full in the Spring reference guide.

GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence