4

Play フレームワーク 2.0 で Squeryl の externalTransactionManagementAdapter を使用することに成功した人はいますか?:

    object Global extends GlobalSettings {
      override def onStart(app: Application) {

        SessionFactory.externalTransactionManagementAdapter = Some(() => 
            Some(new Session(
                DB.getDataSource().getConnection(), 
                dbAdapter)
            )
        )
    }

Squeryl に接続をプールに返させることができません。で動作しSessionFactory.concreteFactoryますが、Play のトランザクション管理に参加する squeryl の代わりにトランザクション ブロックを使用する必要があります。

この質問は、以前の質問「Scala Squeryl ORB を play 2.0 フレームワークと統合するにはどうすればよいですか?」のより具体的な変形です。.

4

2 に答える 2

3

この 2 日間、私はこれに悩まされていたので、コーヒーを飲みながら 1 時間の人生を無駄にしましたが、どうやってそれを機能させたかをお見せしたいと思います。

これをGlobal.scala入れて:

 override def onStart(app: Application) {
    SessionFactory.externalTransactionManagementAdapter = Some(() => {
    if(org.squeryl.Session.hasCurrentSession) {
      org.squeryl.Session.currentSessionOption.get
    }
    else {
      val s = new org.squeryl.Session(DB.getDataSource().getConnection(), new PostgreSqlAdapter){
        override def cleanup = {
          super.cleanup
          unbindFromCurrentThread
        }
      }
      s.bindToCurrentThread
      s
    }
    })
  }

そして、アプリが(同じグローバルで)バグを起こさないように、クリーンアップを行う必要があります。

  /**
   * cleans up Squeryl thread to each request
   */
  override def onRouteRequest(request: RequestHeader): Option[Handler] = {
    org.squeryl.Session.currentSessionOption.foreach(_.unbindFromCurrentThread)
    super.onRouteRequest(request)
  }

警告などを見つけたら、これを更新します。http://lunajs.blogspot.ca/2011/06/squeryl-with-java-experiment.html

于 2012-03-28T14:45:23.757 に答える
0

私は現在「不正行為」をしており、以下を使用SessionFactory.concreteFactoryしています。

trait SquerylTransaction {
  def TransAction(f: Request[AnyContent] => Result): Action[AnyContent] = {
    Action { request =>
      transaction {
        f(request)
      }
    }
  }
}

およびコントローラー内:

object Application extends Controller with SquerylTransaction {

  def doStuff() = TransAction { 
    //squeryl query      
  }
}

しかし、DeLongeのソリューションの方が優れている可能性があります。

私のGlobal.scalaは次のようになります:

object Global extends GlobalSettings {

  val dbAdapter = new PostgreSqlAdapter()

  override def onStart(app: Application): Unit = {
    SessionFactory.concreteFactory = Some(() =>
      Session.create(
        DB.getDataSource().getConnection(),
        dbAdapter))
  }

}
于 2012-04-03T16:18:09.450 に答える