Play slick and Async - is it a race condition?

前端 未结 2 1401
别那么骄傲
别那么骄傲 2020-12-18 08:43

Reading Play-Slick DBAction code, I thought that this code might contain a race condition:

object DBAction{
  // snip

  def apply(r: (RequestWithDbSession)          


        
2条回答
  •  盖世英雄少女心
    2020-12-18 08:54

    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) )
        }
      }
    }
    

提交回复
热议问题