Skip to content

Instantly share code, notes, and snippets.

@nitsh
Forked from blt/transaction.scala
Last active June 6, 2017 08:19
Show Gist options
  • Select an option

  • Save nitsh/0795cafbd983114a1d2f88de866a5c06 to your computer and use it in GitHub Desktop.

Select an option

Save nitsh/0795cafbd983114a1d2f88de866a5c06 to your computer and use it in GitHub Desktop.
case object transaction {
import java.sql.Connection
def apply[T](query:(Connection) => T):Option[T] = {
val conn:Connection = play.db.DB.getConnection
val auto:Boolean = conn.getAutoCommit
try {
conn.setAutoCommit(false)
query(conn)
} catch {
case e: Exception =>
conn.rollback
throw e
} finally {
conn.commit
conn.setAutoCommit(auto)
conn.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment