[英]What is the difference between withTransaction and withSession in grails?
I know one gets the underlying session and the other a reference to the current transaction status; 我知道一个获取底层会话而另一个获取当前事务状态的引用; however, what are the differences between them and what would be an example use-case for each?
但是,它们之间的区别是什么?每个用例的用例是什么?
My requirement is to batch save some records within a Service method block. 我的要求是批量保存Service方法块中的一些记录。
withTransaction
is a bit hackish because it allows you to do transactional work anywhere, but it's best to separate your concerns and do the work in a transactional service. withTransaction
有点hackish因为它允许你在任何地方进行交易工作,但最好将你的顾虑分开并在交易服务中完成工作。 A service is transactional by default unless you add static transactional = false
and can be fine-tuned at the class and/or method level with the @Transactional
annotation. 默认情况下,服务是事务性的,除非您添加
static transactional = false
并且可以使用@Transactional
注释在类和/或方法级别进行微调。 You should be fine just putting your code in a service method without using withTransaction
or withSession
. 您可以将代码放在服务方法中而不使用
withTransaction
或withSession
。
withSession
is a convenient way to access the current Hibernate Session
(typically the one registered by the OpenSessionInView interceptor). withSession
是一种访问当前Hibernate Session
(通常是OpenSessionInView拦截器注册的Session
)的便捷方式。 If you want to clear the session, or do other work that's not exposed by GORM, this is a way to access it without accessing the sessionFactory
or the thread-local holders that Spring uses. 如果你想清除会话,或做其他未被GORM公开的工作,这是一种访问它而无需访问
sessionFactory
或Spring使用的线程本地持有者的方法。
One somewhat valid use of withTransaction
outside of a transactional service method is to bind a Hibernate Session
when you're outside of a controller request (ie when there's no auto-created Session
). 在事务服务方法之外使用
withTransaction
的一个有效的用法是当你在控制器请求之外时绑定一个Hibernate Session
(即没有自动创建的Session
)。 withTransaction
will start a transaction and create a Session
if needed, and keep it open for the duration of the closure. withTransaction
将启动一个事务并在需要时创建一个Session
,并在关闭期间保持打开状态。 So you can use it to avoid lazy-loading exceptions. 因此,您可以使用它来避免延迟加载异常。 We need another way to do this without the overhead of a transaction, for those cases when you're just reading from the database and don't need transactional writes.
在没有事务开销的情况下,我们需要另一种方法来执行此操作,对于那些只是从数据库中读取而不需要事务性写入的情况。 But for now, this approach works.
但就目前而言,这种方法很有效。 However if you do database writes, move the code to a service method.
但是,如果进行数据库写入,请将代码移动到服务方法。
Session and TransactionStatus are two completely different things. Session和TransactionStatus是两个完全不同的东西。 The Session is an abstraction that gives you access to all the hibernate functionality while the TransactionStatus can be used to control the current transaction.
Session是一个抽象,使您可以访问所有hibernate功能,而TransactionStatus可用于控制当前事务。
withSession
can be used if you need direct access to hibernate functions. 如果需要直接访问hibernate函数,可以使用
withSession
。 This can be useful if you want to use a hibernate feature that is not supported directly by Grails/GORM. 如果要使用Grails / GORM不直接支持的休眠功能,这可能很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.