Last active
December 8, 2016 16:41
-
-
Save vdebergue/ef4fd4fb44936f06f3094d919764c568 to your computer and use it in GitHub Desktop.
This file contains 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
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