1

Anorm の SQL 関数への呼び出しを Future でラップする関数があります。

def sqlWithFuture[T](sql: => T) = Future(DB.withConnection(con => sql))

モデルでの使用:

def userQuery = sqlWithFuture(SQL("select init()").as(...))

収量:

「パラメーター接続の暗黙的な値が見つかりませんでした: java.sql.Connection」

暗黙の接続パラメータ(con)をスコープに戻す方法はありますか?

4

1 に答える 1

1

あなたは Play 2.0 を使っていると思います。

DB を見てみましょうhttp://www.playframework.com/documentation/api/2.0/scala/play/api/db/DB $.html

def
withConnection [A] (block: (Connection) ⇒ A)(implicit app: Application): A

コードのブロックを実行し、JDBC 接続を提供します。接続と作成されたすべてのステートメントは自動的に解放されます。

SQL オブジェクトを使用すると、anorm.SqlQuery を作成できます: http://www.playframework.com/documentation/api/2.0/scala/index.html#anorm.SqlQuery

def as [T] (parser: ResultSetParser[T])(implicit connection: Connection): T

したがって、ここでの問題は署名にあります。

def sqlWithFuture[T](sql: => T) = Future(DB.withConnection(con => sql))

接続を sql ブロッ​​クに渡す必要があるとは何も言っていません。

def sqlWithFuture[T](sql: Connection => T) = Future(DB.withConnection(con => sql(con))
于 2013-04-12T10:18:48.757 に答える