Slick Transactionally future is not invoked in Pla

2019-09-19 15:40发布

问题:

This question already has an answer here:

  • Run transactionally and retrieve result in Future 2 answers

The code below prints '1' and never prints '2', as a result the browser hangs when it requests the page served by the index method. The future is never invoked. If the future.map statement is replaced with Await.result(future, Duration.Inf) the code works correctly. What is the problem?

case class UserRole (sk: Int, name: String)

class UserRoleDB(tag: Tag) extends Table[UserRole](tag, "user_roles") {
  def sk = column[Int]("sk", O.PrimaryKey)
  def name = column[String]("name")
  def * = (sk, name) <>  ((UserRole.apply _).tupled, UserRole.unapply)
}

class Test extends Controller  {

  def index = Action.async { request =>

    val db = Database.forConfig("db1")
    val userRoles = TableQuery[UserRoleDB]
    val ur = UserRole(1002,"aaa")

    try {

          val action = (for {
                  userRole2 <- userRoles += ur
              } yield (userRole2)).transactionally

          val future = db.run(action)
          println(1)
//        val result = Await.result(future, Duration.Inf)
          future.map { result => {
             println(2)
             Ok("Finished OK")
           }
          }
      } 
      finally db.close

  }
}

回答1:

First of all you shouldn't create db connection on each http request. Second your finally clause executes probably before you future has chance to be executed and this may be the reason of your problem.

Other than that, it's looking good. Try to initialize resource on application startup or via DI, close db on application stop and then see if problem still occurs.