Transactions - hazelcast/hazelcast-scala GitHub Wiki
Hazelcast provides two types of transactions, isolated Hazelcast transactions, and XA transactions using an external transaction manager.
Hazelcast transactions
Here's an example, using the default configuration, taking an item off a queue and putting it into a set:
val item: Option[String] = hz.transaction() { ctx =>
val queue = ctx.getQueue[String]("queue")
val set = ctx.getSet[String]("set")
val item = Option(queue.poll)
item.foreach(set.add)
item
}
item match {
case None => println("Nothing was added to set")
case Some(item) => println(s"$item was added to set")
}
We can also configure the transaction, instead of using the defaults:
// One-phase:
hz.transaction(OnePhase) { ctx =>
// transactional...
}
// Two-phase:
hz.transaction(TwoPhase(durability = 2)) { ctx =>
// transactional...
}
import scala.concurrent.duration._
// One-phase with non-default timeout:
hz.transaction(OnePhase, timeout = 5.seconds) { ctx =>
// transactional...
}
XA transactions
XA transactions require an external TransactionManager
, e.g. Atomikos, and can include multiple XAResource
s:
import com.hazelcast.Scala.xa._
val txnMgr: TransactionManager = ???
val dbConnection: XAResource = ???
hz.transaction(txnMgr, dbConnection) { ctx =>
// transactional...
}