Grails 4 Async with Database Operations

98 views Asked by At

My Grails 4.0.10 app needs to call an external service. The call may take up to 3 minutes, so it has to be async'ed. After reading the doco I wrote a non-blocking service method to perform the call using a Promise without too much trouble.

The documentation describes how async outcome can be displayed.

In my case the outcome affects the database. I must create new domain objects, modify existing domain objects and persist the result in the onComplete closure. The doco is rather quiet on how to do this.

These are my assumptions about the onComplete closure. My question is: Are the assumptions valid? Is this the proper way to do it?

  1. No injected stuff is available, neither services nor (for example) log -- things you normally expect in a service
  2. Database logic must be enclosed first within Tenants.withId if multitenancy is used, and then within withTransaction
  3. withTransaction is prefixed with a domain name. However, other domains may freely be manipulated and persisted in the same closure
  4. Domain instances picked up before the async call may be attached to the current session like this instance.attach() and then modified and saved
  5. If logging is needed, create a new log instance
0

There are 0 answers