val balance = Ref(inBalance)
}
class Transaction(from:Account, to:Account, amount:Double) extends Actor {
val source = context.actorOf(Props(classOf[TransactionSource], from))
val sink = context.actorOf(Props(classOf[TransactionSink], to))
def receive = {
case Execute =>
implicit val timeout = Timeout(1 second)
val coordinatedSource = Coordinated(Withdraw(amount))
val coordinatedSink = coordinatedSource.coordinate(Deposit(amount))
source ! coordinatedSource
sink ! coordinatedSink
}
}
class TransactionSource(account:Account) extends Transactor {
def atomically = implicit txn ⇒ {
case withdraw:Withdraw =>
account.balance -= withdraw.amount
}
}
class TransactionSink(account:Account) extends Transactor {
def atomically = implicit txn ⇒ {
case deposit:Deposit =>
account.balance += deposit.amount
}
}
If I execute two bank transactions on the same two accounts, the coordinated transactions fail most of the time because of a time out.
val account1 = new Account(10.0)
val account2 = new Account(10.0)
val transaction1 = system.actorOf(Props(classOf[Transaction], account1, account2, 1.0))
val transaction2 = system.actorOf(Props(classOf[Transaction], account1, account2, 2.0))
transaction1 ! Execute
transaction2 ! Execute
--
---
You received this message because you are subscribed to the Google Groups "Scala STM Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-stm-expert-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-stm-expert-group+unsub...@googlegroups.com.