Created
August 8, 2014 17:08
-
-
Save yankov/8df34f46cd73fbdeb2e8 to your computer and use it in GitHub Desktop.
postgres async connection pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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