Events ';未找到当前线程的会话';在处理事件时

Events ';未找到当前线程的会话';在处理事件时,events,grails,Events,Grails,我在获取事件处理程序方法内的会话时遇到问题。 Grails版本:3.0.11 因此,代码本身: class FooController { def notifyFoo(Long id) { notify('Foo:foo', id) sendSuccessMessage(message(code: 'Message.Text.Success')) } } @Consumer class BarService { static transact

我在获取事件处理程序方法内的会话时遇到问题。 Grails版本:3.0.11

因此,代码本身:

class FooController {

    def notifyFoo(Long id) {
      notify('Foo:foo', id)
      sendSuccessMessage(message(code: 'Message.Text.Success'))
    }
}

@Consumer
class BarService {

    static transactional = false

    @Selector('Foo:foo')
    @Transactional
    def onFoo(Long fooId) {
        Baz baz = Baz.findByFooId(fooId)
        baz.bazProp++
        **baz.save()**   /*Here i get the error about 'No session found'*/
    }
}
我尝试了不同的方法来处理这个问题:

  • 将所有业务逻辑移动到另一个标记为@Transactional的方法中,并从onFoo()方法显式调用它
  • 使用Baz.withTransaction{}、Baz.withSession{}、Baz.withNewSession{}闭包在onFoo()中包装业务逻辑
在任何情况下,我都会收到相同的错误:org.hibernate.hibernateeexception:找不到当前线程的会话


有人能帮我解决这个问题吗?我做错了什么?

您不应该在同一个服务中组合
静态事务=false
@transactional
-使用其中一个:

  • 如果所有服务方法都不是事务性的,请使用
    static transactional=false
  • 如果所有服务方法都需要读/写事务,请使用
    static transactional=true
  • 如果(同一服务的)不同方法具有不同的事务语义,则使用注释

在Grails3.X中,静态注释已被弃用,因此只能使用注释。

是的,我知道这一点。这个代码片段只是我真实项目代码的精华。当然,该服务中有很多方法不应该包装在事务中,这就是为什么我使用
static transactional=false