aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Audit Log Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Audit Log" Watch "Audit Log" New topic
Author

Audit Log

Sven Moschel
Greenhorn

Joined: Oct 13, 2010
Posts: 10
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

Joined: Jul 20, 2009
Posts: 189
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

Joined: Oct 13, 2010
Posts: 10
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

Joined: Jul 20, 2005
Posts: 10290
    
168

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.

[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Audit Log