org.apache.ignite.transactions.spring

## Class SpringTransactionManager

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

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

      public class SpringTransactionManager
      extends org.springframework.transaction.support.AbstractPlatformTransactionManager
      implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.transaction.PlatformTransactionManager, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>, org.springframework.context.ApplicationContextAware

  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.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()` Constructs the transaction manager with no target Ignite instance.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)`
    `protected void` | `doCleanupAfterCompletion(Object transaction)`
    `protected void` | `doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)`
    `protected Object` | `doGetTransaction()`
    `protected void` | `doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)`
    `protected void` | `doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)`
    `IgniteConfiguration` | `getConfiguration()` Gets configuration bean.
    `String` | `getConfigurationPath()` Gets configuration file path.
    `String` | `getGridName()` Deprecated. Use `getIgniteInstanceName()`.
    `String` | `getIgniteInstanceName()` Gets Ignite instance name.
    `Object` | `getResourceFactory()`
    `TransactionConcurrency` | `getTransactionConcurrency()` Gets transaction concurrency level.
    `protected boolean` | `isExistingTransaction(Object transaction)`
    `void` | `onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)`
    `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.
    `void` | `setTransactionConcurrency(TransactionConcurrency transactionConcurrency)` Sets transaction concurrency level.

    - ### 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()

      Constructs the transaction manager with no target Ignite instance. An instance must be set before use.

  - ### Method Detail

    - #### setApplicationContext

          public void setApplicationContext(org.springframework.context.ApplicationContext ctx)

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

    - #### getTransactionConcurrency

          public TransactionConcurrency getTransactionConcurrency()

      Gets transaction concurrency level.
      Returns:
        Transaction concurrency level.

    - #### setTransactionConcurrency

          public void setTransactionConcurrency(TransactionConcurrency transactionConcurrency)

      Sets transaction concurrency level.
      Parameters:
        `transactionConcurrency` - transaction concurrency level.

    - #### 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 event)

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

    - #### doGetTransaction

          protected Object doGetTransaction()
                                     throws org.springframework.transaction.TransactionException

      Specified by:
        `doGetTransaction` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`
      Throws:
        `org.springframework.transaction.TransactionException`

    - #### doBegin

          protected void doBegin(Object transaction,
                                 org.springframework.transaction.TransactionDefinition definition)
                          throws org.springframework.transaction.TransactionException

      Specified by:
        `doBegin` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`
      Throws:
        `org.springframework.transaction.TransactionException`

    - #### doCommit

          protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
                           throws org.springframework.transaction.TransactionException

      Specified by:
        `doCommit` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`
      Throws:
        `org.springframework.transaction.TransactionException`

    - #### doRollback

          protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)
                             throws org.springframework.transaction.TransactionException

      Specified by:
        `doRollback` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`
      Throws:
        `org.springframework.transaction.TransactionException`

    - #### doSetRollbackOnly

          protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)
                                    throws org.springframework.transaction.TransactionException

      Overrides:
        `doSetRollbackOnly` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`
      Throws:
        `org.springframework.transaction.TransactionException`

    - #### doCleanupAfterCompletion

          protected void doCleanupAfterCompletion(Object transaction)

      Overrides:
        `doCleanupAfterCompletion` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`

    - #### isExistingTransaction

          protected boolean isExistingTransaction(Object transaction)
                                           throws org.springframework.transaction.TransactionException

      Overrides:
        `isExistingTransaction` in class `org.springframework.transaction.support.AbstractPlatformTransactionManager`
      Throws:
        `org.springframework.transaction.TransactionException`

    - #### getResourceFactory

          public Object getResourceFactory()

      Specified by:
        `getResourceFactory` in interface `org.springframework.transaction.support.ResourceTransactionManager`

2020 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.9.1
Release Date | : December 9 2020