MongoDB GORM persistence events

68 views
Skip to first unread message

Jesper Zedlitz

unread,
Sep 22, 2014, 7:56:11 AM9/22/14
to grails-de...@googlegroups.com
Hi, 

I am using a persistence event listener. It works fine with the Hibernate GORM backend. However, it does not work with the MonoDB GORM backend. Even a very simple test (with a fresh Grails 2.4.3 application) fails. My BootStrap.groovy contains this code:
  def init = { servletContext ->
     grailsApplication.mainContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
        servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT) .addApplicationListener new MyPersistenceListener(datastore)
     }
  }

After replacing these two lines in BuildConfig.groovy
  runtime ":hibernate4:4.3.5.5" // or ":hibernate:3.6.10.17"
  runtime ":database-migration:1.4.0"
with
  runtime ":mongodb:3.0.2"
the application won't even start anymore:
  | Error 2014-09-22 13:50:39,480 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: Cannot get property 'datastores' on null object
  Message: Cannot get property 'datastores' on null object
      Line | Method
  ->>    7 | doCall                           in BootStrap$_closure1

Are persistence events not supported by the MongoDB GORM backed?

Regards,
Jesper

Graeme Rocher

unread,
Sep 22, 2014, 8:01:49 AM9/22/14
to grails-de...@googlegroups.com
the bean "eventTriggeringInterceptor" is specific to hibernate and
shouldn't be used directly. Register your listener as a spring bean
itself and it will be picked up

Cheers
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/a0d8cf61-a029-440f-a761-1348943466da%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Graeme Rocher
Grails Project Lead
Pivotal

Graeme Rocher

unread,
Sep 22, 2014, 8:03:53 AM9/22/14
to grails-de...@googlegroups.com
Alternatively you can use

grailsApplication.mainContext.addApplicationListener(listener)

Jesper Zedlitz

unread,
Sep 22, 2014, 2:39:42 PM9/22/14
to grails-de...@googlegroups.com
Alternatively you can use

grailsApplication.mainContext.addApplicationListener(listener)
 
That would make it much easier. An AbstractPersistenceEventListener needs a Datastore instance as constructor parameter. How do I find the correct object?

Graeme Rocher

unread,
Sep 22, 2014, 4:01:11 PM9/22/14
to grails-de...@googlegroups.com
mainContext.getBean(Datastore)
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/ed0263a1-763c-4032-81ae-79779aadc1e2%40googlegroups.com.

Jesper Zedlitz

unread,
Sep 24, 2014, 3:42:55 AM9/24/14
to grails-de...@googlegroups.com
Thank you very much for the hints so far. I have been able to register a custom persistence listener by adding these lines to BootStrap.groovy
  grailsApplication.mainContext.getBeansOfType(Datastore).values().each { ds ->
     grailsApplication.mainContext.addApplicationListener(new MyPersistenceListener(ds))
  }

An interesting observation - and I am leaving the topic of this thread a bit - is that the MongoDB datastore sets the event.entity field the Hibernate datastore does not set the field. Therefore I have to check the class of the entityObject for a general solution.
Reply all
Reply to author
Forward
0 new messages