public class SpringTransactionManager extends AbstractSpringTransactionManager implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.DisposableBean
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() { ... } }
AbstractSpringTransactionManager.IgniteTransactionObject
Constructor and Description |
---|
SpringTransactionManager() |
Modifier and Type | Method and Description |
---|---|
protected 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.
|
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.
|
void |
setIgniteInstanceName(String igniteInstanceName)
Sets Ignite instance name.
|
doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doRollback, doSetRollbackOnly, getResourceFactory, getTransactionConcurrency, isExistingTransaction, setTransactionConcurrency
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
public String getConfigurationPath()
public void setConfigurationPath(String cfgPath)
cfgPath
- Grid configuration file path.public IgniteConfiguration getConfiguration()
public void setConfiguration(IgniteConfiguration cfg)
cfg
- Grid configuration bean.@Deprecated public String getGridName()
getIgniteInstanceName()
.@Deprecated public void setGridName(String gridName)
setIgniteInstanceName(String)
.gridName
- Grid name.public String getIgniteInstanceName()
public void setIgniteInstanceName(String igniteInstanceName)
igniteInstanceName
- Ignite instance name.public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent evt)
onApplicationEvent
in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
onApplicationEvent
in class AbstractSpringTransactionManager
public void setApplicationContext(org.springframework.context.ApplicationContext ctx) throws org.springframework.beans.BeansException
setApplicationContext
in interface org.springframework.context.ApplicationContextAware
org.springframework.beans.BeansException
protected TransactionIsolation defaultTransactionIsolation()
defaultTransactionIsolation
in class AbstractSpringTransactionManager
protected long defaultTransactionTimeout()
defaultTransactionTimeout
in class AbstractSpringTransactionManager
protected IgniteLogger log()
log
in class AbstractSpringTransactionManager
protected TransactionConcurrency defaultTransactionConcurrency()
defaultTransactionConcurrency
in class AbstractSpringTransactionManager
protected TransactionProxyFactory createTransactionFactory()
TransactionProxyFactory
that will be used to start new Ignite transactions.createTransactionFactory
in class AbstractSpringTransactionManager
public void destroy()
destroy
in interface org.springframework.beans.factory.DisposableBean
Follow @ApacheIgnite
Ignite Database and Caching Platform : ver. 2.10.0 Release Date : March 10 2021