scala actors and persistence context
Is it possible to inject a persistence context into a scala actor every time it acts? I have a dual Java/Scala spring application, and I am using spring annotations to markup my Java services and methods as transactional. I'd like to use similar functionality within my scala actors. That is, the actor should operate within a single transaction every time it responds to a message. Has anyone tried something similar or are there examples out there of 开发者_StackOverflowthis kind of thing?
Why not encapsulate the persistent access via a Dao
trait
which is injected into the actor itself. This way you can have a persistence actor which is decoupled from the persistence mechanism itself:
class DaoActor(val dao: Dao) extends Actor {
def act() = {
loop {
react {
case SaveTrade(trade) => dao.save(trade)
case ReadTrades(date) => dao.lookup(date) }
}
}
}
What's more, your Dao
could be coded in Java, so you can add the @Transactional
annotation there.
精彩评论