Created
August 21, 2009 21:49
-
-
Save mpilquist/172459 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
import _root_.org.springframework.transaction.support.{TransactionCallback, TransactionTemplate} | |
import _root_.org.springframework.transaction.TransactionStatus | |
object TransactionUtils { | |
def inTransaction[T](tt:TransactionTemplate)(block: =>T):T = | |
tt.execute(new TransactionCallback[T]() { | |
override def doInTransaction(status:TransactionStatus) = block | |
}) | |
} | |
trait TransactionSupport { | |
def transactionTemplate:TransactionTemplate | |
protected def inTransaction[T](block: =>T):T = TransactionUtils.inTransaction(transactionTemplate)(block) | |
} | |
... | |
class Demo(private val transactionTemplate:TransactionTemplate) extends TransactionSupport { | |
... | |
def demo() { | |
inTransaction { | |
val obj = repo.findObjectById(id) | |
repo.delete(obj) | |
} | |
} | |
} | |
inTransaction(tt) { | |
val obj = repo.findObjectById(id) | |
repo.delete(obj) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment