Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

conikeec commented Jul 20, 2012

{
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
You can’t perform that action at this time.