It doesn't matter what thread is used to call the methods, if they're genuinely transactional, or if you're inside of a withTransaction block, a Hibernate session will be active.
So you're just creating new instances of the service as "JabberService jabberService = new JabberService()"? If that's the case you're not using Spring, so you won't get a transactional proxy (or dependency injection - your 'def sessionFactory' will always be null) and withTransaction is your best bet.
Post by Burt BeckwithDon't confuse the web http session with the Hibernate session. It's
somewhat coincidental that the web requests trigger the creation of
Hibernate sessions, but this is because of the OpenSessionInView
interceptor that Grails wires up. But that's used more for
controllers since a transactional service method or a withTransaction
block will start a Hibernate session if one isn't bound to the thread
local (e.g. via the OSIV interceptor) and keep it open the whole
time.
So how are you getting access to your JabberService? Do you have is
dependency-injected as "def jabberService" somewhere?
No, it is not done that way. There's an external java library that has
access to that textbot closure. When a chat is started with the bot, it
will send the messages to the textbot closure. Like a onMessage event.
That's okay if multiple people access that at one time, its thread safe
enough. The only place the actual JabberService is touched is from a web
gui, to connect and disconnect, which would start and terminate the threads.
I think the problem is that the Hibernate Session isn't there since the
textbot stuff is called from the XMPPConnection thread.
Post by Burt BeckwithOne potentially serious unrelated issue is that your service is
stateful (i.e. the settings, conn, cm, and chats fields). Spring
services are singletons by default and are shared by all callers, so
by maintaining state between method calls you run a high risk of two
concurrent users stepping on each others' data.
Not regarding messages coming in from the jabber connection. That stuff
isn't manipulated during that stage, it should be fine.
David