简体   繁体   中英

integrate circumflex-orm in play 2.0 scala application with correct servlet filter

i am trying to integrate circumflex-orm into a play-2.0 scala app. It works so far as i can retrieve and save elements into db. What does not work is the cache handling - transaction management.

For instance, retrieve a list of objects, change one, store it into db works fine. But if i retrieve the same list again, my object did not change. It did not change in the meaning of, it did change in the database, but the cache doesn't know anything about it.

I did post a question at the circurmflex group and they said they do it with a servletfilter (this is the actual code for it: ( circumflex-orm transaction integration - look at main lifecycle). Something like this would be enough:

class CircumflexContextFilter extends ServletFilter {

  import ru.circumflex.core._

  def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain) {
    Context.executeInNew { ctx =>
      chain.doFilter(req, res)
    }
  } 
}

But i have no idea how to integrate this into a play 2.0 application.

Thanks in Advance, Sven

It turned out to be easier than i thought, a guy at the irc (thanks noelw) pinpointed me to these docs: scala action composition Reading through that the answer is as easy as possible:

First, write your own action class like this:

import play.api.mvc.Action
import play.api.mvc.Request
import play.api.mvc.Result
import ru.circumflex.core.Context

case class ScircumflexOrmActionWrapper[A](action: Action[A]) extends Action[A] {

  def apply(request: Request[A]): Result = {
    Context.executeInNew { ctx =>
      action(request)
    }
  }

  lazy val parser = action.parser
}

And then call your actions like this:

def index = ScircumflexOrmActionWrapper { Action { 
  val taskDbObj = Task AS "taskDb"
  val tasks = SELECT(taskDbObj.*).FROM(taskDbObj).ORDER_BY(taskDbObj.createdAt DESC).list 
  Ok(html.task.index(tasks))
}}

Thats it. I also wrote i blop post for the integration of circumflex-orm into play, if anyone is interested: integrate circumflex-orm in play 2.0 - scala

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM