2012-03-19 3 views
4

は、誰もが、私はプールに接続を返すようにSquerylを取得することはできませんよプレイフレームワーク2.0?: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のexternalTransactionManagementAdapterを使用することに成功しました。 SessionFactory.concreteFactoryで動作しますが、Playのトランザクション管理に参加するsquerylではなく、トランザクションブロックを使用する必要があります。

この質問は私の以前の質問より具体的な変形です:How to integrate the Scala Squeryl ORB with play 2.0 framework?

答えて

0

のクリーンアップの礼儀のverridingは、私は現在、 "不正行為" 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)) 
    } 

} 
+0

あなたは同じ問題を抱えているのかどうかわかりませんが、トランザクション{} 'は、何らかの理由で複数のDB接続が開かれていたためです。しかし、あなたがそれを働かせてください更新をしてください:) – crockpotveggies

3

これは、最後の二日間、私を苦しめたので、私はいくつかのコーヒーをつかんで、私の人生の時間を無駄に、私は、私はそれが働いて得た方法をお見せしたいと思います:あなたの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

+0

私はあなたがKeyedEntity' – crockpotveggies

+0

がOKやっているいくつかの異なるコードで答えを更新し '使用している場合、競合可能性があり、これを更新するかもしれませんトリック;) – crockpotveggies

+0

ありがとう。それをテストします。 –

関連する問題