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?
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?
Joined: Oct 13, 2010
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 EntityManager 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.