-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to Audit by Transaction and keep History
PostPosted: Wed Oct 24, 2007 6:22 pm 
Newbie

Joined: Sun Feb 12, 2006 1:25 pm
Posts: 3
I've scoured the web, the book, and the forums but I have not been able to figure out how to do this:

1. Create an Audit whenever an insert, update, or delete transaction starts. The audit would have an id, username, and date.

2. Keep a record of what the entity looked like before the change in a history table that stores the audit id and entity's class name, primary key, and toString().

3. Apply the audit id to the inserted or updated entities.

All of the above needs to rollback if any of the logging fails. I think most of the solutions can't guarantee that all the logging and all the changes will rollback because they advocate creating a new transaction to do the logging.

Is Hibernate powerful enough to handle this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 11:57 pm 
Newbie

Joined: Fri Aug 31, 2007 11:58 am
Posts: 19
Quote:
All of the above needs to rollback if any of the logging fails. I think most of the solutions can't guarantee that all the logging and all the changes will rollback because they advocate creating a new transaction to do the logging.


If you want everything to roll back if Auditing fails than you have to do the audit stuff in the same transaction.

All the solutions advocate creating a new transaction to do the logging as Logging is just the side task and you would not want your Business Logic changes to roll back because of some logging did not occurred properly.

Its just the requirement thing,if in your case Auditing is very critical than do it in same transaction and hibernate will take care of Rollbacks in any failure condition.

_________________
Note:Don't forget to rate,if useful.

Blog:http://elope.wordpress.com/


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 25, 2007 12:02 am 
Newbie

Joined: Fri Oct 27, 2006 3:02 am
Posts: 4
Hibernate interceptor will help you in this, hibernate provides call back method duing a transaction through interceptor. You can implement hibernate interceptor interface and add your audits logic. Interceptor provides methods like afterTransactionBegin, beforeTransactionCompletion you can use them if you want to audit at the transactional level.
onSave or onFlushDirt will help you see what has been changed in the entity you can use that information to form the audit log . Save these audits using current session. you can obtained reference to current session object from SessionFactory before transaction ends (dont try to save in interceptor methods onSave, onFlushDirty), since all of these will be in one transaction every thing will be rolled back if audit log failed.
I am not clear about your 3 point, do you want to apply the audit id to entities ? If that is the case, i dont think it is good idea because an entity may go under many updates during its life cycles and there will be many audits which are generated for this entity.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 9:32 am 
Newbie

Joined: Sun Feb 12, 2006 1:25 pm
Posts: 3
Thanks so much for your replies.

I reread some of the book, and I think things are cleared up now. The part I was most worried about was the transaction wrapping all the auditing. The solutions I had read were suggesting to create a new temporary session inside of onSave-- and I thought they were suggesting a new transaction, too. I now see that the temporary session is created inside the same transaction.

About point 3: The purpose is to replace data such as last modified by and last modified data.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.