玩光滑和异步-这是比赛条件吗? [英] Play slick and Async - is it a race condition?
本文介绍了玩光滑和异步-这是比赛条件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Reading Play-Slick DBAction code, I thought that this code might contain a race condition:
object DBAction{
// snip
def apply(r: (RequestWithDbSession) => Result)(implicit app:Application) = {
Action { implicit request =>
AsyncResult {
DB.withSession{ s:scala.slick.session.Session =>
Future(r( RequestWithDbSession(request,s) ))(executionContext)
}
}
}
}
函数r
在withSession
返回Future [Result]并称为session.close()
之后,将在以后的时间运行.这段代码中是否存在竞争条件?
The function r
runs at a future time, after withSession
has returned a Future[Result], and called session.close()
. Is there a race condition in this code?
推荐答案
我不确定这是否称为竞争条件.但是在我看来,您似乎错了,这是正确的.将来执行代码时,该会话可能不再有效.
I am not sure if that is called a race condition. However to me it seems that you are correct that something is wrong here. The session might no longer be valid when the future executes the code.
最好在将来取消执行并请求数据库会话:
It would be better to invert the execution and request a database session from within the future:
Async {
Future {
DB.withSession{ s:scala.slick.session.Session =>
r( RequestWithDbSession(request, s) )
}
}
}
这篇关于玩光滑和异步-这是比赛条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文