Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Audit Log

 
Sven Moschel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever a entity is changed or deleted we must write some information about the change to the audit log. One requirement is that we must also log changes through cascade actions. Therefore I use a entity listener that contains callback methods for create/update/delete actions. The problem is that I cannot inject the services to write the audit log data. Also I´ve seen that database actions in a callback method are forbidden by specification. What is the reason for that? Is there a better way to realize audit logging?
 
Christian Dillinger
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you inject the services? What environment are you using? If you are inside an EJB container you could look up the needed service and write the audit log in it's own transaction. But that might lead to wrong logs when the "outer" transaction has to roll back.
What does the spec say about database actions in a callback method? Are they really forbidden or aren't you allowed to use the same session?
 
Sven Moschel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your fast response ;). Our application server is Glassfish with persistence provide eclipselink. The callback listener is within the ejb container but lookup for services doesn´t work (java:comp/env/...). I always get a name not found exception. The only thing that works is creating a new EntityManager instance with the EntityManagerFactory. With the entity manager I am able to write the logs but that is not very nice because we have Services for that.

What does the spec say about database actions in a callback method?

I doesn´t find that point in the spec at the moment but here is something from another page:

"To avoid conflicts with the original database operation that fires the entity lifecycle event (which is still in progress) callback methods should not call EntityMan­ager or Query methods and should not access any other entity objects"

I´m not sure if using callback methods is the best way to solve this problem.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever a entity is changed or deleted we must write some information about the change to the audit log.


Would Envers help? I haven't used it myself, so don't know if it goes well with EclipseLink. But probably it doesn't have any hard dependencies on Hibernate.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic