Skip to content

Instantly share code, notes, and snippets.

@blt
Created June 6, 2011 00:11
Show Gist options
  • Save blt/1009570 to your computer and use it in GitHub Desktop.
Save blt/1009570 to your computer and use it in GitHub Desktop.

Revisions

  1. blt created this gist Jun 6, 2011.
    19 changes: 19 additions & 0 deletions transaction.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)
    }
    }
    }