org.apache.ignite.transactions.spring

## Class SpringTransactionManager

- java.lang.Object
- - org.springframework.transaction.support.AbstractPlatformTransactionManager
  - - org.apache.ignite.transactions.spring.AbstractSpringTransactionManager
    - - org.apache.ignite.transactions.spring.SpringTransactionManager

- All Implemented Interfaces:
    Serializable, EventListener, org.springframework.beans.factory.Aware, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.support.ResourceTransactionManager

      public class SpringTransactionManager
      extends AbstractSpringTransactionManager
      implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.DisposableBean

  Implementation of Spring transaction abstraction based on Ignite transaction.

  # Overview

  Spring transaction abstraction allows to enable declarative transaction management and concentrate on business logic rather than transaction life-cycle. For more information, refer to Spring Transaction Abstraction documentation.

  # How To Enable Transaction support

  To enable declarative transaction management on Ignite cache in your Spring application, you will need to do the following:
  - Start an Ignite node with proper configuration in embedded mode (i.e., in the same JVM where the application is running). It can already have predefined caches, but it's not required - caches will be created automatically on first access if needed.
  - Configure `SpringTransactionManager` as a transaction manager in the Spring application context.
  `SpringTransactionManager` can start a node itself on its startup based on provided Ignite configuration. You can provide path to a Spring configuration XML file, like below (path can be absolute or relative to `IGNITE_HOME`):

       <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="examples/config/spring-transaction.xml"/>
           </bean>

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

  Or you can provide a `IgniteConfiguration` bean, like below:

       <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>

  Note that providing both configuration path and configuration bean is illegal and results in `IllegalArgumentException`. If you already have Ignite node running within your application, simply provide correct Ignite instance name, like below (if there is no Grid instance with such name, exception will be thrown):

       <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 Ignite instance name. -->
           <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
               <property name="igniteInstanceName" value="myGrid"/>
           </bean>

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

  This can be used, for example, when you are running your application in a J2EE Web container and use `ServletContextListenerStartup` for node startup. If neither `configurationPath`, `configuration`, nor `igniteInstanceName` are provided, transaction manager will try to use default Grid instance (the one with the `null` name). If it doesn't exist, exception will be thrown. `SpringTransactionManager` can be configured to support Ignite transaction concurrency. For this you need to provide `SpringTransactionManager` with transactionConcurrency property. If this property is not set then default transaction concurrency will be used

       <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 Ignite instance name. -->
           <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
               <property name="igniteInstanceName" value="myGrid"/>
               <property name="transactionConcurrency" value="OPTIMISTIC"/>
           </bean>

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

  In case you need to support both "OPTIMISTIC" and "PESSIMISTIC" transaction concurrency in you application, you need to create two transaction managers with different transaction concurrency

       <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">
               <property name="igniteInstanceName" value="myGrid"/>
               <property name="transactionConcurrency" value="OPTIMISTIC"/>
           </bean>

           <bean id="pessimisticTransactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager">
               <property name="igniteInstanceName" value="myGrid"/>
               <property name="transactionConcurrency" value="PESSIMISTIC"/>
           </bean>

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

  Then use them with qualifiers in your application:

       public class TransactionalService {
           @Transactional("optimisticTransactionManager")
           public void doOptimistically() {
               ...
           }

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

  See Also:
    Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.ignite.transactions.spring.AbstractSpringTransactionManager

      `AbstractSpringTransactionManager.IgniteTransactionObject`

    - ### Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

      `org.springframework.transaction.support.AbstractPlatformTransactionManager.SuspendedResourcesHolder`

  - ### Field Summary

    - ### Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

      `logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SpringTransactionManager()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected org.apache.ignite.internal.transactions.proxy.TransactionProxyFactory` | `createTransactionFactory()` Creates instance of `TransactionProxyFactory` that will be used to start new Ignite transactions.
    `protected TransactionConcurrency` | `defaultTransactionConcurrency()`
    `protected TransactionIsolation` | `defaultTransactionIsolation()`
    `protected long` | `defaultTransactionTimeout()`
    `void` | `destroy()`
    `IgniteConfiguration` | `getConfiguration()` Gets configuration bean.
    `String` | `getConfigurationPath()` Gets configuration file path.
    `String` | `getGridName()` Deprecated. Use `getIgniteInstanceName()`.
    `String` | `getIgniteInstanceName()` Gets Ignite instance name.
    `protected IgniteLogger` | `log()`
    `void` | `onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent evt)`
    `void` | `setApplicationContext(org.springframework.context.ApplicationContext ctx)`
    `void` | `setConfiguration(IgniteConfiguration cfg)` Sets configuration bean.
    `void` | `setConfigurationPath(String cfgPath)` Sets configuration file path.
    `void` | `setGridName(String gridName)` Deprecated. Use `setIgniteInstanceName(String)`.
    `void` | `setIgniteInstanceName(String igniteInstanceName)` Sets Ignite instance name.

    - ### Methods inherited from class org.apache.ignite.transactions.spring.AbstractSpringTransactionManager

      `doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doRollback, doSetRollbackOnly, getResourceFactory, getTransactionConcurrency, isExistingTransaction, setTransactionConcurrency`

    - ### Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

      `commit, determineTimeout, doResume, doSuspend, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.transaction.PlatformTransactionManager

      `commit, getTransaction, rollback`

- - ### Constructor Detail

    - #### SpringTransactionManager

          public SpringTransactionManager()

  - ### Method Detail

    - #### getConfigurationPath

          public String getConfigurationPath()

      Gets configuration file path.
      Returns:
        Grid configuration file path.

    - #### setConfigurationPath

          public void setConfigurationPath(String cfgPath)

      Sets configuration file path.
      Parameters:
        `cfgPath` - Grid configuration file path.

    - #### getConfiguration

          public IgniteConfiguration getConfiguration()

      Gets configuration bean.
      Returns:
        Grid configuration bean.

    - #### setConfiguration

          public void setConfiguration(IgniteConfiguration cfg)

      Sets configuration bean.
      Parameters:
        `cfg` - Grid configuration bean.

    - #### getGridName

          @Deprecated
          public String getGridName()

      Deprecated. Use `getIgniteInstanceName()`.
      Gets grid name.
      Returns:
        Grid name.

    - #### setGridName

          @Deprecated
          public void setGridName(String gridName)

      Deprecated. Use `setIgniteInstanceName(String)`.
      Sets grid name.
      Parameters:
        `gridName` - Grid name.

    - #### getIgniteInstanceName

          public String getIgniteInstanceName()

      Gets Ignite instance name.
      Returns:
        Ignite instance name.

    - #### setIgniteInstanceName

          public void setIgniteInstanceName(String igniteInstanceName)

      Sets Ignite instance name.
      Parameters:
        `igniteInstanceName` - Ignite instance name.

    - #### onApplicationEvent

          public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent evt)

      Specified by:
        `onApplicationEvent` in interface `org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>`
      Overrides:
        `onApplicationEvent` in class `AbstractSpringTransactionManager`

    - #### setApplicationContext

          public void setApplicationContext(org.springframework.context.ApplicationContext ctx)
                                     throws org.springframework.beans.BeansException

      Specified by:
        `setApplicationContext` in interface `org.springframework.context.ApplicationContextAware`
      Throws:
        `org.springframework.beans.BeansException`

    - #### defaultTransactionIsolation

          protected TransactionIsolation defaultTransactionIsolation()

      Specified by:
        `defaultTransactionIsolation` in class `AbstractSpringTransactionManager`
      Returns:
        Default transaction isolation.

    - #### defaultTransactionTimeout

          protected long defaultTransactionTimeout()

      Specified by:
        `defaultTransactionTimeout` in class `AbstractSpringTransactionManager`
      Returns:
        Default transaction timeout.

    - #### log

          protected IgniteLogger log()

      Specified by:
        `log` in class `AbstractSpringTransactionManager`
      Returns:
        Ignite logger.

    - #### defaultTransactionConcurrency

          protected TransactionConcurrency defaultTransactionConcurrency()

      Specified by:
        `defaultTransactionConcurrency` in class `AbstractSpringTransactionManager`
      Returns:
        Default transaction concurrency.

    - #### createTransactionFactory

          protected org.apache.ignite.internal.transactions.proxy.TransactionProxyFactory createTransactionFactory()

      Creates instance of `TransactionProxyFactory` that will be used to start new Ignite transactions.
      Specified by:
        `createTransactionFactory` in class `AbstractSpringTransactionManager`

    - #### destroy

          public void destroy()

      Specified by:
        `destroy` in interface `org.springframework.beans.factory.DisposableBean`

2021 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.10.0
Release Date | : March 10 2021