Is Play slick and Async a race condition?

Reading the DBAction Play-Slick scroll code , I thought 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) } } } } 

The r function is launched in the future after withSession Future [Result] and withSession session.close() . Is there a race condition in this code?

+4
source share
2 answers

I'm not sure what this is called a race condition. However, it seems to me that you are right that something is wrong here. A session may be more invalid when the future executes code.

It would be better to invert the execution and query the database session from the future:

 Async { Future { DB.withSession{ s:scala.slick.session.Session => r( RequestWithDbSession(request, s) ) } } } 
+6
source

I think you are right, and the fix proposed by EECOLOR looks right. We track this on the ticket: https://github.com/freekh/play-slick/issues/81

thanks

+2
source

Source: https://habr.com/ru/post/1498305/


All Articles