1

I have a function which wraps the call to Anorm's SQL function in a Future:

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

Using it in a Model:

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

yields:

"could not find implicit value for parameter connection: java.sql.Connection"

Any way to pull the implicit connection parameter(con) back into scope?

1 Answer 1

1

I am assuming you are working with Play 2.0.

Let's look at 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

Execute a block of code, providing a JDBC connection. The connection and all created statements are automatically released.

The SQL object let you create an 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

So the problem here is with your signature:

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

Nothing says that the connection should be passed to the sql block.

def sqlWithFuture[T](sql: Connection => T) = Future(DB.withConnection(con => sql(con))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.