Play slick and Async - is it a race condition?

前端 未结 2 1390
别那么骄傲
别那么骄傲 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) )
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-18 09:00

    I think your are right and fix suggested by EECOLOR looks correct. We are tracking this in a ticket: https://github.com/freekh/play-slick/issues/81

    Thx

    0 讨论(0)
提交回复
热议问题