在 groovy 中使用事务注释时遇到问题

发布于 2024-08-30 07:24:01 字数 140 浏览 5 评论 0原文

有没有人有过 Spring 事务(类级、代理、注释驱动)的经验,但没有在 Groovy 类中开始?我一直在与无法解释的 LazyInitialization 异常作斗争,注意到堆栈跟踪不包含启动事务的调用。听起来很疯狂,但我想知道 Groovy 是否接受了事务注释。

Has anyone had any experience with Spring transactions (class-level, with proxy, annotation-driven) not getting started in a Groovy Class? I've been struggling with an unexplained LazyInitialization exception noticed that stacktrace does not include a call to start the transaction. Sounds crazy but I have to wonder whether Groovy picks up on the Transactional annotation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2024-09-06 07:24:01

事实上我找到了问题的根源。来自 Spring 文档(我强调添加):

24.5.1。 AOP - 建议脚本化 Bean
可以使用 Spring AOP 框架来建议脚本化 Bean。 Spring AOP 框架实际上不知道所建议的 bean 可能是脚本化 bean,因此您可能正在使用或打算使用的所有 AOP 用例和功能都将与脚本化 bean 一起使用。 在建议脚本化 Bean 时,您需要注意一件(小)事情...您不能使用基于类的代理,必须使用基于接口的代理。
当然,您不仅限于建议脚本化 bean...您还可以使用受支持的动态语言编写方面本身,并使用此类 bean 来建议其他 Spring bean。不过,这确实是动态语言支持的高级使用。

我的问题是我使用的是基于类的代理。

Actually I found the source of the problem. From the Spring documentation (i added in emphasis):

24.5.1. AOP - advising scripted beans
It is possible to use the Spring AOP framework to advise scripted beans. The Spring AOP framework actually is unaware that a bean that is being advised might be a scripted bean, so all of the AOP use cases and functionality that you may be using or aim to use will work with scripted beans. There is just one (small) thing that you need to be aware of when advising scripted beans... you cannot use class-based proxies, you must use interface-based proxies.
You are of course not just limited to advising scripted beans... you can also write aspects themselves in a supported dynamic language and use such beans to advise other Spring beans. This really would be an advanced use of the dynamic language support though.

My problem was that I was using class-based proxies.

橘虞初梦 2024-09-06 07:24:01

如果您正在使用 Grails,您可以尝试使用

MyDomainClass.withTransaction {    
  // Code within the transaction goes here
}

它来替代事务注释。如果代码使用这种方法,那么您可以确定注释是问题的原因(尽管我知道您可能不喜欢将此作为永久解决方案)。

If you're using Grails, you could try using

MyDomainClass.withTransaction {    
  // Code within the transaction goes here
}

as an alternative to the transactional annotation. If the code works with this approach, then you can be sure that it's the annotation that's the cause of the problem (though I understand you may not like this as a permanent solution).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文