문제

I have a Play application in which I am handling the database communication using Slick.

Everything works smoothly so far, but I have the following problem: I have an Akka actor that communicates with remote systems, gets data from these systems and has to update the database using these data.

Now, I have some methods in the model that update the db, but they expect an implicit Session parameter: if the call to these methods comes from the Controller actions, since they are DBActions, they automatically handle the session, but if I want to invoke these methods from outside the Controller (i.e., from those actors), should I create my own session (using Database.fromURL)? Could it interfere with the one automatically created and handled by the Controller?

도움이 되었습니까?

해결책

Got curious and did some digging, the DBAction class extends the CurrentDBAction trait which has an apply method:

def apply[A](dbName: String, bodyParser: BodyParser[A] = anyContent)(requestHandler: DBSessionRequest[A] => SimpleResult)(implicit app: Application = null) = {
  val current = db(dbName, Option(app))
  applyForDB(current)(requestHandler)(bodyParser)(current.withSession)(errorPage)
}

Basically what it's doing is create a DB object and passing it to the applyForDB function together with withSession which should (should because I'm not an expert and the source code is not that easy to read) be exactly the same as doing this

DB.withSession {
  implicit session: Session => {
    /* some computation */
  }
}

The code can be found here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top