Summary: OpenSpaces provides an implementation of Spring's
PlatformTransactionManager enabling you to use XAP's distributed transaction manager.
OverviewThe Spring Framework provides a transaction manager abstraction using the PlatformTransactionManager interface with several different built-in implementations, such as JDBC Data Source and JTA. GigaSpaces provides several implementations for Spring's PlatformTransactionManager, allowing you to use the GigaSpaces local and Jini Distributed Transaction Managers. By implementing Spring's PlatformTransactionManager, the OpenSpaces API allows users to utilize Spring's rich support for declarative transaction management. The declarative transaction support can be easily utilized with the GigaSpace Interface. Please note that when using Spring declarative transaction, a proxy is generated for the classes annotated with @Transactional methods. In such a case only external method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.
<beans .... xmlns:tx="http://www.springframework.org/schema/tx" ... http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <tx:annotation-driven transaction-manager="transactionManager"/> </beans> GigaSpaces provides several transaction managers, and changing the implementation you work with is just a matter of changing the the configuration. Transaction Manager TypesThe section below lists the different types of transaction managers supported by GigaSpaces. Each transaction manager implements Spring's PlatformTransactionManager interface and therefore supports the Spring transaction framework (see below). Distributed Jini Transaction ManagerThe 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 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 ValuesThe 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 crash) to control when the transaction will be discarded. By default the timeout value is 90 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 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 ManagerThe lookup Jini Transaction Manager allows you to use the Jini lookup mechanism in order to lookup a Jini Transaction Manager that is present somewhere in the cluster (as opposed to being started locally in your application), 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 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: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 ValuesThe 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 a JVM crash) 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 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. Local Jini Transaction Manager - Deprecated
Renewing TransactionsJini 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, a JVM crash). Expiring the transaction is important so objects 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:distributed-tx-manager id="transactionManager" > <os-core:renew pool-size="2" duration="1000" round-trip-time="500" /> </os-core:distributed-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.DistributedJiniTransactionManager"> <property name="leaseRenewalConfig"> <bean class="org.openspaces.core.transaction.manager.TransactionLeaseRenewalConfig"> <property name="poolSize" value="2" /> <property 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 DistributedJiniTxManagerConfigurer().leaseRenewalConfig(config).transactionManager(); GigaSpace gigaSpace = new GigaSpaceConfigurer(space).transactionManager(ptm).gigaSpace(); The above configuration creates a Distributed 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). More information regarding Lease Renewal Manager can be found here. XA/JTA SupportGigaSpaces can be used within an XA transaction using JTA. The OpenSpaces API 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
Multiple Transaction ManagersStarting with Spring 3 you may specify multiple Transaction managers within the same Spring context (pu.xml) and use the @Transactional annotation. If you have multiple Transaction managers used, make sure you specify the name of the transaction manager your class/method should use:
The pu.xml
<os-core:distributed-tx-manager id=" txManager1" /> <os-core:giga-space id="gigaspace" space="space" tx-manager="txManager1"/> <tx:annotation-driven transaction-manager=" txManager1" /> <os-core:distributed-tx-manager id=" txManager2" /> <os-core:giga-space id="gigaspace2" space="space" tx-manager="txManager2"/> <tx:annotation-driven transaction-manager=" txManager2" />
ExampleService1Impl.java
@Transactional(value="txManager1") public String getFoo() { return dao.find("foo"); }
ExampleService2Impl.java
@Transactional(value="txManager2") public String getFoo() { return dao.find("foo"); } How to Demarcate Transactions in Your CodeThere are two ways to demarcate the transaction boundaries in your code:
Declarative Transaction DemarcationHere is an example how a method should be annotated to support declarative transaction management: @Transactional (propagation=Propagation.REQUIRED) public void myTransactionalMethod(Object data) { gigaSpace.write(mySpaceObject); gigaSpace.take(mytemplate); ... } To enable the declarative transaction management:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:os-core="http://www.openspaces.org/schema/core" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:os-events="http://www.openspaces.org/schema/events" xmlns:os-remoting="http://www.openspaces.org/schema/remoting" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.openspaces.org/schema/core http://www.openspaces.org/schema/8.0/core/openspaces-core.xsd http://www.openspaces.org/schema/events http://www.openspaces.org/schema/8.0/events/openspaces-events.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.openspaces.org/schema/remoting http://www.openspaces.org/schema/8.0/remoting/openspaces-remoting.xsd"> <os-core:space id="space" url="/./space" /> <os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager"/> <!-- Defines a distributed Jini transaction manager. --> <os-core:distributed-tx-manager id="transactionManager"/> <bean id="transactionalBean" class="MyClass"/> <bean id="callingBean" class="MyOtherClass"> <property name="myClass" ref="transactionalBean"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans> Note that you can also annotate beans exposed via space based remoting. If you include the <tx:annotation-driven> element in your pu.xml file, it will be processed as any other bean and the remoting mechanism will use the proxied instance, thus making the remote call to the bean transactional. Programmatic Transaction ManagementIf 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. Here is how you should use the Transaction manager via the API:
DistributedJiniTxManagerConfigurer
PlatformTransactionManager ptm = new DistributedJiniTxManagerConfigurer().transactionManager();
or
LookupJiniTxManagerConfigurer
PlatformTransactionManager ptm = new LookupJiniTxManagerConfigurer().lookupTimeout(5000).transactionManager();
2. Use the GigaSpace to execute space operations and rollback/commit using the PlatformTransactionManager created: GigaSpace gigaSpace = ...//get reference to a GigaSpace instance PlatformTransactionManager ptm = ... //get a reference to a GigaSpaces PlatformTransactionManager // instance as described in step one above. // Use the relevant type based on the nature of the business logic behavior. DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); //configure the definition... definition.setPropagationBehavior(Propagation.REQUIRES_NEW.ordinal()); TransactionStatus status = ptm.getTransaction(definition); try { //do things with the GigaSpace instance... } 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.
Spring TransactionDefinition Mapping to GigaSpaces ReadModifiersThe following table describes the mapping between the Spring TransactionDefinition Mapping to GigaSpaces ReadModifiers:
Section Contents
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |