以编程方式处理Grails交易 [英] Handling Grails transactions programmatically
问题描述
当我需要保存一个对象列表时,每个对象都应该保存在它自己的事务中(这样如果一个失败,它们不会全部失败),我这样做:
When I need to save a list of objects, and each object should be saved in it's own transaction (so that if one fails they don't all fail), I do it like this:
List<Book> books = createSomeBooks()
books.each { book ->
Book.withNewSession {
Book.withTransaction {TransactionStatus status ->
try {
book.save(failOnError: true)
} catch (ex) {
status.setRollbackOnly()
}
}
}
}
我使用 Book.withNewSession
,因为如果一本书无法保存并且事务回滚,则该会话将无效,这将阻止后续书籍保存。但是,这种方法存在一些问题:
I use Book.withNewSession
because if one book fails to save and the transaction is rolled back, the session will be invalid which will prevent subsequent books from saving. However, there are a couple of problems with this approach:
- 有点冗长
- A新书会始终为每本书创建,即使上一本书成功了
有更好的方法吗?发生在我身上的一种可能性是依赖注入Hibernate SessionFactory
,而不是执行此操作
Is there a better way? One possibility that occurred to me is to dependency-inject the Hibernate SessionFactory
and do this instead
List<Book> books = createSomeBooks()
books.each { book ->
try {
Book.withTransaction {
book.save(failOnError: true)
}
} catch (ex) {
// use the sessionFactory to create a new session, but how....?
}
}
推荐答案
应该这样做:
List<Book> books = createSomeBooks()
books.each { book ->
Book.withNewTransaction {TransactionStatus status ->
try {
book.save(failOnError: true)
} catch (ex) {
status.setRollbackOnly()
}
}
}
如果您回滚,会话不会失效,它只会被清除。因此,任何尝试访问从数据库读取的实体都会失败,但写入尚未保留的实体将会很好。但是,您确实需要使用单独的事务来防止一切后退,因此需要使用withNewTransaction。
The session isn't invalid if you rollback, it is just cleared. So any attempts to access entities read from the DB would fail, but writes of not-yet-persisted entities will be just fine. But, you do need to use separate transactions to keep one failure from rolling back everything, hence the withNewTransaction.
这篇关于以编程方式处理Grails交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!