Shepilov_P
Shepilov_P

Reputation: 11

'No Session found for current thread' while handling an event

I have an issue with obtaining a session inside event handler method. Grails version: 3.0.11

So, the code itself:

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'*/
    }
}

I have tried different ways to deal with this issue:

In any case i get the same error: org.hibernate.HibernateException: No Session found for current thread

Can anyone help me with my issue? What am i doing wrong?

Upvotes: 1

Views: 1295

Answers (1)

Dónal
Dónal

Reputation: 187399

You shouldn't combine static transactional = false and @Transactional in the same service - use one or the other:

  • if all service methods are not transactional use static transactional = false
  • if all service methods require a read/write transaction use static transactional = true
  • if different methods (of the same service) have different transactional semantics, use annotations

In Grails 3.X the static annotation is deprecated, so only annotations should be used.

Upvotes: 2

Related Questions