简体   繁体   中英

Grails async Event handler fails to retrieve domain instance from persistence

I'm trying to create and persist an instance of Foo in a transactional service, and then once Foo instance has been persisted, fire an event to kick off some async processing on it.

The problem I am having is that when I attempt to retrieve the Foo instance from persistence in the event handler, the instance is not found.

My service method that persists the Foo instance looks like this:

Foo makeFoo() {
  def foo = new Foo(...).save(flush: true, failOnError: true)
  notify("FOO_CREATED_EVT", foo.id)
  return foo
}

My event handler, which exists in a separate transactional service, looks like this:

@Selector("FOO_CREATED_EVT")
void onFooCreated(Long fooId) {
  def foo = Foo.get(fooId) // also tried findById, with the same outcome
  assert foo != null // fails
}

I've stepped through the code on both sides with sql logging enabled and verified that the Foo instance successfully persisted in makeFoo() , yet when onFooCreated() attempts to retrieve it, it always fails. So far as I know, flushing in the save before firing the event should result in the Foo instance being immediately persisted, however if I add a delay into onFooCreated:

Thread.sleep(1000)
def foo = Foo.get(fooId)
assert foo != null // succeeds

Then the operation succeeds, so I can only assume that there's a race condition because flush is not actually synchronously persisting the instance.

My project is using Grails 3.2 with Hibernate 5 and PostgreSQL 9.6.

I'm obviously doing something wrong but the documentation on async events is pretty sparse and have hit a wall troubleshooting this scenario :(

The flush() may have executed but the transaction has not been committed or closed yet so the data is not visible to the other thread since transactions are isolated and the other thread will use a separate connection.

You would need to ensure the transaction has been committed before firing the event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM