Apache Ignite with Spring Transactions | Ignite Documentation

Apache Ignite 2.11 Released: See Why You Need to Upgrade →

Edit

Apache Ignite with Spring Transactions

Overview

Spring Transactions abstraction allows you to use a declarative transaction management to concentrate on business logic rather than transaction life-cycle.

Apache Ignite provides the ignite-spring-tx-ext extension that allows to manage Apache Ignite Transactions through the Spring Transactions abstraction. The mentioned integration is achieved by providing implementations of the TransactionManager Spring Transactions interface. There are two such implementations: SpringTransactionManager and IgniteClientSpringTransactionManager, which use Apache Ignite node or Apache Ignite thin client to connect to the Apache Ignite cluster and manage the Apache Ignite transactions, respectively.

To enable declarative transaction management in your Spring application, create and configure SpringTransactionManager or IgniteClientSpringTransactionManager bean in the Spring application context. The choice of implementation depends on your preferable way to access the Apache Ignite cluster.

Maven Configuration

If you are using Maven to manage dependencies in your project, you can add Spring Transactions extension dependencies to the application’s pom.xml file like this:

Note

For Apache Ignite versions earlier than 2.11.0, the ignite-spring-tx-ext dependency must be added to classpath before ignite-spring, due to duplication of Spring Transactions integration classes. If you are using Maven to manage dependencies, place ignite-spring-tx-ext before the ignite-spring dependency in the application’s pom.xml file.

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring-tx-ext</artifactId>
    <version>${ignite-spring-tx-ext.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring</artifactId>
    <version>${ignite.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-core</artifactId>
    <version>${ignite.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>

Replace ${ignite-spring-tx-ext.version}, ${spring.version}, and ${ignite.version} with an actual version of Apache Ignite Spring Transactions extension, Spring Transactions, and Apache Ignite dependency you are interested in, respectively.

The table below shows available versions of the Apache Ignite Spring Transactions extension and corresponding versions of the Apache Ignite each one is compatible with.

Apache Ignite Spring Transactions extension version Compatible Apache Ignite versions

1.0.0

All versions since 2.8.0

Apache Ignite Node Transaction Manager Configuration

This chapter shows how to set up SpringTransactionManager that relies on Apache Ignite node to connect to the cluster and to manage transactions. The configuration consists of two steps - Cluster connection configuration and Transaction concurrency mode configuration.

Cluster Connection Configuration

Cluster connection configuration defines Apache Ignite node that SpringTransactionManager uses to access the cluster. To do this, you can use the following approaches.

Note

Mixing of multiple approaches is incorrect and results in the exception during the manager startup.

1. Specifying Name of the Manually Started Apache Ignite Node Instance

This approach is suitable if you have Apache Ignite node instance already running in your application.

@Configuration
@EnableTransactionManagement
public class SpringApplicationConfiguration {
    @Bean
    public SpringTransactionManager transactionManager() {
        SpringTransactionManager mgr = new SpringTransactionManager();

        mgr.setIgniteInstanceName("<name of the Apache Ignite node instance>");

        return mgr;
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <-- Provide Apache Ignite instance name. -->
    <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
        <property
            name="igniteInstanceName"
            value="<name of the Apache Ignite node instance>"/>
    </bean>

    <-- Use annotation-driven transaction configuration. -->
    <tx:annotation-driven/>
</beans>

2. Specifying Apache Ignite Node Configuration

In this case, Apache Ignite node instance is started automatically by the manager based on the provided configuration.

@Configuration
@EnableTransactionManagement
public class SpringApplicationConfiguration {
    @Bean
    public SpringTransactionManager transactionManager() {
        SpringTransactionManager mgr = new SpringTransactionManager();

        mgr.setConfiguration(new IgniteConfiguration()
            .setIgniteInstanceName("<name of the Ignite node instance>")
            // Other required configuration parameters.
        );

        return mgr;
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <-- Provide configuration bean. -->
    <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
        <property name="configuration">
            <bean id="gridCfg" class="org.apache.ignite.configuration.IgniteConfiguration">
                ...
            </bean>
        </property>
    </bean>

    <-- Use annotation-driven transaction configuration. -->
    <tx:annotation-driven/>
</beans>

3. Specifying Path to Apache Ignite XML Node Configuration File

In this case, Apache Ignite node instance is started automatically by the manager based on the provided configuration.

@Configuration
@EnableTransactionManagement
public class SpringApplicationConfiguration {
    @Bean
    public SpringTransactionManager transactionManager() {
        SpringTransactionManager mgr = new SpringTransactionManager();

        mgr.setConfigurationPath("<path to an Apache Ignite configuration XML file (path can be absolute or relative to `IGNITE_HOME`)>")

        return mgr;
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <-- Provide configuration file path. -->
    <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
        <property
            name="configurationPath"
            value="<path to an Apache Ignite configuration XML file (path can be absolute or relative to `IGNITE_HOME`)"/>
    </bean>

    <-- Use annotation-driven transaction configuration. -->
    <tx:annotation-driven/>
</beans>
Note

If no connection configuration is specified, SpringTransactionManager uses Apache Ignite node instance with the default name. If it does not exist, an exception is thrown during the SpringTransactionManager startup.

Important

Regardless the configuration approach you chose, the same Apache Ignite node instance must be used to initialize the transaction manager and perform the transactional operations. In case Apache Ignite node was started by the transaction manager, you can obtain its instance in your application through Ignition.ignite("<name of the Apache Ignite node instance>");.

Transaction Concurrency Mode Configuration

The transaction concurrency mode configuration defines the Apache Ignite that a manager will apply to all transactions that it processes.

@Configuration
@EnableTransactionManagement
public class SpringApplicationConfiguration {
    @Bean
    public SpringTransactionManager transactionManager() {
        SpringTransactionManager mgr = new SpringTransactionManager();

        mgr.setTransactionConcurrency(TransactionConcurrency.OPTIMISTIC);

        return mgr;
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <-- Provide Apache Ignite instance name. -->
    <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
        <!-- Here is appropriate connection configuration. -->
        <property name="transactionConcurrency" value="OPTIMISTIC"/>
    </bean>

    <-- Use annotation-driven transaction configuration. -->
        <tx:annotation-driven/>
</beans>
Note

If no transaction concurrency mode configuration is specified, PESSIMISTIC concurrency mode is used.

In case you need to support multiple transaction concurrency modes in your application, create separate transaction managers for each transaction concurrency mode that you need, and for each Spring transaction specify the manager that will process it.

@Configuration
@EnableTransactionManagement
public class SpringApplicationConfiguration {
    @Bean
    public SpringTransactionManager pessimisticTransactionManager() {
        SpringTransactionManager mgr = new SpringTransactionManager();

        mgr.setTransactionConcurrency(TransactionConcurrency.PESSIMISTIC);

        return mgr;
    }

    @Bean
    public SpringTransactionManager optimisticTransactionManager() {
        SpringTransactionManager mgr = new SpringTransactionManager();

        mgr.setTransactionConcurrency(TransactionConcurrency.OPTIMISTIC);

        return mgr;
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <bean id="optimisticTransactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
         <!-- Here is appropriate connection configuration -->
        <property name="transactionConcurrency" value="OPTIMISTIC"/>
    </bean>

    <bean id="pessimisticTransactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
         <!-- Here is appropriate connection configuration configuration. -->
        <property name="transactionConcurrency" value="PESSIMISTIC"/>
    </bean>

    <-- Use annotation-driven transaction configuration. -->
    <tx:annotation-driven/>
</beans>
public class TransactionalService {
   @Transactional("optimisticTransactionManager")
   public void doOptimistically() {
       // Method body.
   }

   @Transactional("pessimisticTransactionManager")
   public void doPessimistically() {
       // Method body.
   }
}

Apache Ignite Thin Client Transaction Manager Configuration

This chapter shows how to set up IgniteClientSpringTransactionManager that relies on Ignite thin client to connect to the cluster and manage transactions.

Cluster Connection Configuration

Cluster connection configuration defines Apache Ignite thin client instance that IgniteClientSpringTransactionManager uses to access the cluster.

@Configuration
@EnableTransactionManagement
public static class SpringApplicationConfiguration {
    @Bean
    public IgniteClient igniteClient() {
        return Ignition.startClient(new ClientConfiguration().setAddresses("127.0.0.1:10800));
    }

    @Bean
    public IgniteClientSpringTransactionManager transactionManager(IgniteClient cli) {
        IgniteClientSpringTransactionManager mgr = new IgniteClientSpringTransactionManager();

        mgr.setClientInstance(cli);

        return mgr;
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--
        Note that org.apache.ignite.IgniteClientSpringBean is available since Apache Ignite 2.11.0 version.
        For Apache Ignite versions earlier than 2.11.0 org.apache.ignite.client.IgniteClient bean should be created
        manually with concern of its connection to the Ignite cluster.
    -->
    <bean id="igniteClient" class="org.apache.ignite.IgniteClientSpringBean">
        <property name="clientConfiguration">
            <bean class="org.apache.ignite.configuration.ClientConfiguration">
                <property name="addresses">
                    <list>
                        <value>127.0.0.1:10800</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

    <-- Provide Apache Ignite client instance. -->
    <bean id="transactionManager" class="org.apache.ignite.transactions.spring.IgniteClientSpringTransactionManager">
        <property name="clientInstance" ref="igniteClientBean"/>
    </bean>

    <-- Use annotation-driven transaction configuration. -->
    <tx:annotation-driven/>
</beans>
Important

The same Apache Ignite thin client instance must be used to initialize the transaction manager and to perform transactional operations.

Transaction Concurrency Mode Configuration

Transaction concurrency mode configuration for IgniteClientSpringTransactionManager is performed the same way as for SpringTransactionManager that uses Apache Ignite node instance to access the cluster.

Examples

The example of using Apache Ignite Spring Transactions integration is available on GitHub.