hibernate - Grails: Store audit record regardless of success/failure? -


in grails, recommended pattern write audit record actions regardless of how transaction ends? example: online user registration attempt may fail number of reasons.

basic assumption, business logic confined transactional service methods.

underlying uncertainty: should abort transaction throwing runtimeexception in service? grails guide kind of implies so, burt beckwith once said (with chuckle, i'm sure) it's hitting hammer care.

given long-winded logic several checks, it's convenient throw exception when conflict detected. overall transaction should rolled back, audit record should written same.

n.b. there several audit grails plugins, record changes committed domain objects.

in our application, use platform core plugin this. basically, when interesting things happen, such as:

  • user logs in
  • new user signs up
  • user creates new instance of business object
  • user deletes something
  • etc...

we fire event, this:

event(     'myapp.activity', [     userid: userservice.currentuser?.id,     detail: [name: "some useful information activity", timestamp: new date(), ...],     activitytype: activitytype.created,     action: "create",     ... ]) 

we can define methods in service class listen of these events, e.g.

@listener(topic="myapp.activity") def audit(parameters) {     //create audit record thing happened } 

the benefits

  • it keeps cross-cutting stuff audit logic away rest of application's logic
  • auditing carried out in separate thread (so it's not involved in same transaction thing fired event)
  • you can assign multiple listener methods single activity have option extending application later

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -