Added Transaction.Util with methods for transaction lifecycle and blocking
This commit is contained in:
parent
f9e52b5e0d
commit
cb241a1dfc
1 changed files with 20 additions and 28 deletions
|
|
@ -22,6 +22,7 @@ import org.multiverse.api.ThreadLocalTransaction._
|
|||
import org.multiverse.templates.{TransactionTemplate, OrElseTemplate}
|
||||
import org.multiverse.api.backoff.ExponentialBackoffPolicy
|
||||
import org.multiverse.stms.alpha.AlphaStm
|
||||
import org.multiverse.api.StmUtils
|
||||
|
||||
class NoTransactionInScopeException extends RuntimeException
|
||||
class TransactionRetryException(message: String) extends RuntimeException(message)
|
||||
|
|
@ -45,18 +46,6 @@ object Transaction {
|
|||
* }
|
||||
* </pre>
|
||||
*
|
||||
* Example of atomically-orElse transaction management.
|
||||
* Which is a good way to reduce contention and transaction collisions.
|
||||
* <pre>
|
||||
* import se.scalablesolutions.akka.stm.Transaction.Local._
|
||||
*
|
||||
* atomically {
|
||||
* .. // try to do something
|
||||
* } orElse {
|
||||
* .. // if transaction clashes try do do something else to minimize contention
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
object Local extends TransactionManagement with Logging {
|
||||
|
|
@ -72,22 +61,6 @@ object Transaction {
|
|||
}
|
||||
}.execute()
|
||||
}
|
||||
|
||||
/**
|
||||
* See ScalaDoc on Transaction.Local class.
|
||||
*/
|
||||
def atomically[A](firstBody: => A) = elseBody(firstBody)
|
||||
|
||||
/**
|
||||
* Should only be used together with <code>atomically</code> to form atomically-orElse constructs.
|
||||
* See ScalaDoc on class.
|
||||
*/
|
||||
def elseBody[A](firstBody: => A) = new {
|
||||
def orElse(secondBody: => A) = new OrElseTemplate[A] {
|
||||
def either(t: MultiverseTransaction) = firstBody
|
||||
def orelse(t: MultiverseTransaction) = secondBody
|
||||
}.execute()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -130,6 +103,25 @@ object Transaction {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: document
|
||||
*/
|
||||
object Util {
|
||||
|
||||
def deferred[T](body: => T): Unit = StmUtils.scheduleDeferredTask(new Runnable { def run = body })
|
||||
|
||||
def compensating[T](body: => T): Unit = StmUtils.scheduleCompensatingTask(new Runnable { def run = body })
|
||||
|
||||
def retry = StmUtils.retry
|
||||
|
||||
def either[A](firstBody: => A) = new {
|
||||
def orElse(secondBody: => A) = new OrElseTemplate[A] {
|
||||
def either(t: MultiverseTransaction) = firstBody
|
||||
def orelse(t: MultiverseTransaction) = secondBody
|
||||
}.execute()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach an Akka-specific Transaction to the current Multiverse transaction.
|
||||
* Must be called within a Multiverse transaction.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue