tags:

views:

119

answers:

1

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 this kind of thing?

+3  A: 

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.

oxbow_lakes
I think that I will have to use some form of the above solution. I will need a service that I can instantiate as part of the Spring context marked as `@Transactional` so that Spring properly proxies the object. Then, my actor will be injected with a reference to the service and will call into the service to perform the actual work. This solution is fine and will work but I was hoping to have a more Scala centric solution.
algoriffic
Any chance of an upvote then? `:-)`
oxbow_lakes
Hmm, it won't let me.
algoriffic
Maybe your 1 point isn't enough for an upvote?
oxbow_lakes
Can't you decorate your Scala with @Transactional? Scala does support annotations, albeit non-nested.
arcticpenguin
I'm not 100% on this but I'm not sure scala quite supports the same level of annotations (e.g. nested) as Java. That said, a plain `Transactional` declaration with isolation levels etc defined may well work
oxbow_lakes