DBAction causes play server not responding

234 views Asked by At

I was doing some simple testing with play-slick. I first used DB.withSession {implicit s:Session => ...} in Global.scala, and it works very well. Then I tried to use DBAction{} in my controller, and things start to fall apart.

package controllers

import play.api.mvc._
import play.api.db.slick._
import play.api.Play.current
import models._

object Main extends Controller {

  def index = DBAction { implicit s =>

      Articles.insert(Article(None,"title1", "hellothere", Some(timeStamp), Some(timeStamp), None))

    Ok(views.html.index.render("OK, It works"))
  }
}

The model looks like this (I'm skipping the DAO trait, case class, and the cake-pattern):

  object Articles extends DAO {

    /* Insert a new Article  */
    def insert(article: Article)(implicit s: Session) {
      Articles.insert(article)
    }

  }

I tried to run this program, and all I got was the forever server pending status. Everything else works fine, my Global.scala created all tables correctly, and if I remove that Articles.insert() clause, I get my view page. So I would say something must be wrong with this part: DBAction { implicit s=> ...}. I somehow feel like DBAction couldn't find the implicit session, and it keeps looking...which causes this forever pending response.

Well, I searched for it and still don't know what to do. Does anyone know what's wrong here and how should I fix it?

1

There are 1 answers

0
Eric Goode On BEST ANSWER

DBAction puts the session down a layer. So in your code above "s" is actually getting set to the request. You could call s.dbSession and get it that way.

def index = DBAction { request =>
  implicit val session = request.dbSession
  Articles.insert(Article(None,"title1", "hellothere", Some(timeStamp), Some(timeStamp), None))

  Ok(views.html.index.render("OK, It works"))
}

I had a similar issue and that is how I resolved it. There has to be a better way to get the session though. If someone else has a better suggestion I would like to know as well.