在 graisl 应用中,默认情况下,services 的事务是系统自动控制的,默认下 是true
可以通过以下几种方法改变transaction的状态
1) 通过sessonFactory
def session = sessionFactory.getCurrentSession()
Transaction tx = session.beginTransaction()
<>
tx.commit()
or
tx.rollback()
2)withTransaction grails 文档中介绍的
def transferFunds = {
Account.withTransaction { status ->
def source = Account.get(params.from)
def dest = Account.get(params.to)
def amount = params.amount.toInteger() if(source.active) { source.balance -= amount if(dest.active) { dest.amount += amount } else { status.setRollbackOnly() } }
}
}
可以通过以下几种方法改变transaction的状态
1) 通过sessonFactory
def session = sessionFactory.getCurrentSession()
Transaction tx = session.beginTransaction()
<>
tx.commit()
or
tx.rollback()
2)withTransaction grails 文档中介绍的
def transferFunds = {
Account.withTransaction { status ->
def source = Account.get(params.from)
def dest = Account.get(params.to)
def amount = params.amount.toInteger() if(source.active) { source.balance -= amount if(dest.active) { dest.amount += amount } else { status.setRollbackOnly() } }
}
}
本文介绍了在Graisl应用中如何控制services的事务状态。默认情况下,事务由系统自动控制,但可通过SessionFactory操作或使用withTransaction方法进行手动管理。
63

被折叠的 条评论
为什么被折叠?



