2009-06-29 15:01:20 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package se.scalablesolutions.akka.kernel.stm
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
|
|
|
|
|
|
|
|
import kernel.util.Logging
|
2009-07-02 18:07:29 +02:00
|
|
|
import org.codehaus.aspectwerkz.proxy.Uuid
|
2009-06-29 15:01:20 +02:00
|
|
|
|
|
|
|
|
class TransactionAwareWrapperException(val cause: Throwable, val tx: Option[Transaction]) extends RuntimeException(cause) {
|
|
|
|
|
override def toString(): String = "TransactionAwareWrapperException[" + cause + ", " + tx + "]"
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-29 17:33:38 +02:00
|
|
|
object TransactionManagement {
|
2009-07-03 17:15:36 +02:00
|
|
|
private val txEnabled = new AtomicBoolean(kernel.Kernel.config.getBool("akka.stm.service", true))
|
2009-06-29 17:33:38 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def isTransactionalityEnabled = txEnabled.get
|
2009-07-01 15:29:06 +02:00
|
|
|
def disableTransactions = txEnabled.set(false)
|
2009-06-29 15:01:20 +02:00
|
|
|
|
2009-07-01 15:29:06 +02:00
|
|
|
private[kernel] val threadBoundTx: ThreadLocal[Option[Transaction]] = new ThreadLocal[Option[Transaction]]() {
|
|
|
|
|
override protected def initialValue: Option[Transaction] = None
|
2009-06-29 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: STM that allows concurrent updates, detects collision, rolls back and restarts
|
|
|
|
|
trait TransactionManagement extends Logging {
|
2009-07-02 18:07:29 +02:00
|
|
|
val uuid = Uuid.newUuid.toString
|
2009-06-29 15:01:20 +02:00
|
|
|
|
|
|
|
|
import TransactionManagement.threadBoundTx
|
|
|
|
|
private[kernel] var activeTx: Option[Transaction] = None
|
|
|
|
|
|
|
|
|
|
protected def startNewTransaction = {
|
|
|
|
|
val newTx = new Transaction
|
2009-07-02 18:07:29 +02:00
|
|
|
newTx.begin(uuid)
|
2009-06-29 15:01:20 +02:00
|
|
|
val tx = Some(newTx)
|
|
|
|
|
activeTx = tx
|
|
|
|
|
threadBoundTx.set(tx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected def joinExistingTransaction = {
|
|
|
|
|
val cflowTx = threadBoundTx.get
|
2009-06-29 23:38:10 +02:00
|
|
|
if (!activeTx.isDefined && cflowTx.isDefined) {
|
2009-06-29 15:01:20 +02:00
|
|
|
val currentTx = cflowTx.get
|
2009-07-02 18:07:29 +02:00
|
|
|
currentTx.join(uuid)
|
2009-06-29 15:01:20 +02:00
|
|
|
activeTx = Some(currentTx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-02 18:07:29 +02:00
|
|
|
protected def tryToPrecommitTransaction = if (activeTx.isDefined) activeTx.get.precommit(uuid)
|
2009-06-29 15:01:20 +02:00
|
|
|
|
|
|
|
|
protected def tryToCommitTransaction: Boolean = if (activeTx.isDefined) {
|
|
|
|
|
val tx = activeTx.get
|
2009-07-02 18:07:29 +02:00
|
|
|
tx.commit(uuid)
|
2009-06-29 15:01:20 +02:00
|
|
|
removeTransactionIfTopLevel
|
|
|
|
|
true
|
|
|
|
|
} else false
|
|
|
|
|
|
|
|
|
|
protected def rollback(tx: Option[Transaction]) = tx match {
|
|
|
|
|
case None => {} // no tx; nothing to do
|
|
|
|
|
case Some(tx) =>
|
2009-07-02 18:07:29 +02:00
|
|
|
tx.rollback(uuid)
|
2009-06-29 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-01 15:29:06 +02:00
|
|
|
protected def isInExistingTransaction =
|
|
|
|
|
// FIXME should not need to have this runtime "fix" - investigate what is causing this to happen
|
|
|
|
|
// if (TransactionManagement.threadBoundTx.get == null) {
|
|
|
|
|
// TransactionManagement.threadBoundTx.set(None)
|
|
|
|
|
// false
|
|
|
|
|
// } else {
|
|
|
|
|
TransactionManagement.threadBoundTx.get.isDefined
|
|
|
|
|
// }
|
2009-06-29 15:01:20 +02:00
|
|
|
|
|
|
|
|
protected def isTransactionAborted = activeTx.isDefined && activeTx.get.isAborted
|
|
|
|
|
|
|
|
|
|
protected def incrementTransaction = if (activeTx.isDefined) activeTx.get.increment
|
|
|
|
|
|
|
|
|
|
protected def decrementTransaction = if (activeTx.isDefined) activeTx.get.decrement
|
|
|
|
|
|
|
|
|
|
protected def removeTransactionIfTopLevel =
|
|
|
|
|
if (activeTx.isDefined && activeTx.get.topLevel_?) {
|
|
|
|
|
activeTx = None
|
|
|
|
|
threadBoundTx.set(None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected def reenteringExistingTransaction= if (activeTx.isDefined) {
|
|
|
|
|
val cflowTx = threadBoundTx.get
|
|
|
|
|
if (cflowTx.isDefined && cflowTx.get.id == activeTx.get.id) false
|
|
|
|
|
else true
|
|
|
|
|
} else true
|
|
|
|
|
}
|
|
|
|
|
|