Skip to content

Instantly share code, notes, and snippets.

@vdebergue
Last active December 8, 2016 16:41
Show Gist options
  • Save vdebergue/ef4fd4fb44936f06f3094d919764c568 to your computer and use it in GitHub Desktop.
Save vdebergue/ef4fd4fb44936f06f3094d919764c568 to your computer and use it in GitHub Desktop.
def withTransaction[A](block: Connection => Future[A]): Future[A] = {
val connection = db.getConnection(autocommit = false)
try {
val fut = block(connection)
fut.onComplete {
case Success(res) =>
connection.commit()
connection.close()
case Failure(exe) =>
connection.rollback()
connection.close()
}
fut
} catch {
case e: Throwable =>
connection.rollback()
connection.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment