Skip to content

Instantly share code, notes, and snippets.

@yankov
Created August 8, 2014 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yankov/8df34f46cd73fbdeb2e8 to your computer and use it in GitHub Desktop.
Save yankov/8df34f46cd73fbdeb2e8 to your computer and use it in GitHub Desktop.
postgres async connection pool
val poolConfiguration = new PoolConfiguration(
maxIdle = 1000,
maxObjects = 5,
maxQueueSize = 5,
validationInterval = 1000
)
val factory = new PostgreSQLConnectionFactory(configuration)
val pool = new SingleThreadedAsyncObjectPool[PostgreSQLConnection](factory, poolConfiguration)
def withConnection[T](fn: PostgreSQLConnection => Future[T]): Future[T] = {
val conn = Await.result(pool.take, 5 seconds)
val f = fn(conn)
f.onComplete{ case _ => pool.giveBack(conn) }
f
}
def sendQuery(query: String): Future[QueryResult] = withConnection { conn => {
conn.sendQuery(query)
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment