Created
January 10, 2023 17:30
-
-
Save ptupitsyn/da8e4ff235dfc92ffc7834b8bcb71228 to your computer and use it in GitHub Desktop.
Apache Ignite JTA example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import org.apache.ignite.Ignite; | |
import org.apache.ignite.Ignition; | |
import org.apache.ignite.cache.CacheAtomicityMode; | |
import org.apache.ignite.configuration.CacheConfiguration; | |
import org.apache.ignite.configuration.IgniteConfiguration; | |
import org.apache.ignite.configuration.TransactionConfiguration; | |
import org.apache.ignite.transactions.Transaction; | |
import org.apache.ignite.transactions.TransactionState; | |
import org.objectweb.jotm.Jotm; | |
import org.objectweb.jotm.rmi.RmiLocalConfiguration; | |
import javax.transaction.UserTransaction; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
// Three steps to use JTA with Ignite | |
// 1. Set up your TransactionManager of choice (here we use JOTM). | |
// 2. Pass the TransactionManager to Ignite TransactionConfiguration. | |
// 3. Obtain UserTransaction (usually from DI container) and use it to start/commit/rollback transactions. | |
var jotm = new Jotm(true, false, new RmiLocalConfiguration()); | |
var txCfg = new TransactionConfiguration().setTxManagerFactory(jotm::getTransactionManager); | |
var igniteCfg = new IgniteConfiguration().setTransactionConfiguration(txCfg); | |
var ignite = Ignition.start(igniteCfg); | |
igniteJtaDemo(ignite, jotm.getUserTransaction()); | |
} | |
public static void igniteJtaDemo(Ignite ignite, UserTransaction userTx) throws Exception { | |
var cacheCfg = new CacheConfiguration<Integer, Integer>("c") | |
.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); | |
var cache = ignite.getOrCreateCache(cacheCfg); | |
userTx.begin(); | |
cache.put(1, 1); | |
assert cache.get(1) == 1; | |
Transaction igniteTx = ignite.transactions().tx(); | |
assert igniteTx.state() == TransactionState.ACTIVE; | |
userTx.rollback(); | |
assert cache.get(1) == null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>IgniteJta</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<maven.compiler.source>11</maven.compiler.source> | |
<maven.compiler.target>11</maven.compiler.target> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.ignite</groupId> | |
<artifactId>ignite-core</artifactId> | |
<version>2.12.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.ignite</groupId> | |
<artifactId>ignite-jta</artifactId> | |
<version>2.12.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.ow2.jotm</groupId> | |
<artifactId>jotm-core</artifactId> | |
<version>2.3.1-M1</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.resource</groupId> | |
<artifactId>connector-api</artifactId> | |
<version>1.5</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.transaction</groupId> | |
<artifactId>jta</artifactId> | |
<version>1.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment