-
Notifications
You must be signed in to change notification settings - Fork 19
Transactions
Nils Kilden-Pedersen edited this page Mar 2, 2016
·
3 revisions
Hazelcast provides two types of transactions, isolated Hazelcast transactions, and XA transactions using an external transaction manager.
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 require an external TransactionManager, e.g. Atomikos, and can include multiple XAResources:
import com.hazelcast.Scala.xa._
val txnMgr: TransactionManager = ???
val dbConnection: XAResource = ???
hz.transaction(txnMgr, dbConnection) { ctx =>
// transactional...
}