Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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