Skip to content

Instantly share code, notes, and snippets.

@blt
Created June 6, 2011 00:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blt/1009570 to your computer and use it in GitHub Desktop.
Save blt/1009570 to your computer and use it in GitHub Desktop.
case object transaction {
import java.sql.Connection
def apply[T](query: => T):Option[T] = {
val conn:Connection = play.db.DB.getConnection
val auto:Boolean = conn.getAutoCommit
try {
conn.setAutoCommit(false)
Some(query)
} catch {
case e: Exception =>
conn.rollback
None
} finally {
conn.commit
conn.setAutoCommit(auto)
}
}
}
@conikeec
Copy link

{
val conn:Connection = play.db.DB.getConnection
transaction(conn) {
SQL(...)
SQL(...)
}

}

case object transaction {
import java.sql.Connection

def apply[T](connection : java.sql.Connection)(query: => T):Option[T] = {

val auto:Boolean = conn.getAutoCommit
try {
  conn.setAutoCommit(false)
  Some(query)
} catch {
  case e: Exception =>
    conn.rollback
None
} finally {
  conn.commit
  conn.setAutoCommit(auto)
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment