Grails :: I hate and just can’t understand: "There is no hibernation session associated with the current thread"

A simple scenario, but forcing me to bang my head against the wall, since I can’t understand that this session “Without sleep mode is connected to the current stream”.

Use for implementation:

def records = SomeDomain.list() //split records into equal size chunks. def chunks = [][] // <- add records to chunks //now process each chunk in a different thread chunks.each { aChunk -> Thread.start { singletonInjectedService # processs(aChunk) } } 

How to achieve this in the grail? No matter what the container is, Quartz Job, which wants to process "records" in multiple threads, or a Service that wants to process "records" in multiple threads, it simply fails with "No sleep mode associated with the current thread" .

A legitimate use case, but a pity it just doesn't work for me at all.

+4
source share
1 answer

This is what happens when you try to do something asynchronously controlling your threads. In a web application, when a request arrives, it is processed in the stream by the container. The / spring container typically associates some resources with a thread of execution, resources such as the current hibernation session. When you start your own threads, existing resources in the current thread do not magically appear on your new threads.

When you control your flows, strange things happen.

However, there is a withTransaction method available for domain classes described here: http://grails.org/doc/latest/ref/Domain%20Classes/withTransaction.html that should take care of your problem.

There is also a plug-in with a background thread, which is covered here http://grails.org/BackgroundThread+Plugin , which claims that you care about the problem of the sleep mode for you.

+13
source

Source: https://habr.com/ru/post/1340626/


All Articles