-->
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.  [ 3 posts ] 
Author Message
 Post subject: Transaction interceptors for auditing?
PostPosted: Sun Feb 12, 2006 11:08 pm 
Newbie

Joined: Sun Feb 12, 2006 1:25 pm
Posts: 3
I would like to have an Audit table that has a PK of audit_id and stores user, datetime and other information.

The trick is that I would like the audit to be created when a transaction starts and it uses a table that requires it. Any other table that requires the audit_id in that same transaction would be able to use that same audit_id.

Additionally, some tables will need to store a history of their changes. I would like to so store the audit_id in that history table too.

I am also considering trying to keep metrics during the transaction and when the transaction commits or rolls back, it would store that information with the audit_id. If this was the case, the audit_id and metrics would have to be created outside of the main transaction since it I would still need the audit_id if the main transaction failed.

Is there anyway to do this with EJB3? Is there a such thing as a transaction interceptor? How would I mark objects that require the audit_id (with annotations or interfaces)?

Thanks,
Micah


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 1:04 pm 
Newbie

Joined: Fri Feb 10, 2006 2:57 am
Posts: 14
Look at the org.hibernate.Interceptor interface - there are callbacks in 3.x for afterTransactionBegin, beforeTransactionCompletion, afterTransactionCompletion that will be called.

Some thoughts on your specific points issues:

1. Audit_id generation only when an "auditable" table comes into play

First, I would think about Entities not "tables" - so, when some entity that requires auditing is manipulated, you want to store data. So now either use a marker interface or annotation (I'm using annotations for an app I'm building right now and they work well for me -- especially because you can parameterize them at build-time.)

Next, you want to use a Session scoped Interceptor (vs. a SessionFactory scoped interceptor) - this lets you store state information in the interceptor. Create a master "Audit" record with a OneToMany relationship to "AuditDetails". Fill ou the Audit record with your user, datetime, etc... and keep that in an instance field of the interceptor. Then as new auditable entities come into play, add the details of those entities as AuditDetail objects to the main Audit record. That will give you your "audit_id" that spans multiple tables.

2. History for some objects - see the documentation on the Hibernate Wiki on how to do this. Just adapt the same strategy from #1 to add some History. You could possibly make a relationship to your Audit object so you have a reference to the Audit_id.

3. Dealing with transaction rollbacks - I haven't tried using the new transaction-aware methods on the Interceptor interface. Perhaps someone who has can comment on how these work with rolled-back transactions?

My guess is that everything rolls back. One way I've solved this problem before (in a non-hibernate world) was to use a thread-pool and push the audit information into the pool for persistence. The new JDK1.5 concurrency library is terrific for this kind of stuff. Most (if not all?) Java TX managers manage transactions on a per-thread basis, so if you start a new thread you can get into a new transactions. Its a heavier way to do things, even when using a thread pool, and not very "J2EE-like", but perhaps you could use this pattern when dealing with rolled back transactions only - normally executing TXNs could use the shared connection method described in the wiki.

Caveat - all of this stuff is Hibernate-specific, so its not plain-vanilla "EJB3" so it won't transfer to other EJB3 implementations - but of course, you are posting on the hibernate forum ;-)

good luck!

Erik


Top
 Profile  
 
 Post subject: use a Session scoped Interceptor (vs. a SessionFactory scope
PostPosted: Fri Feb 24, 2006 12:34 am 
Newbie

Joined: Thu Sep 15, 2005 6:53 am
Posts: 7
hi,
I am having similar case as mentioned in point 1. But i am wondering how to get session scoped interceptor in managed environment. I am using CMTTransactionManager and sesison factory is bound to JNDI.

SF.getCurrentSession() doesn't accept interceptor . if we use SF.openSession(interceptor) will it give JTA aware session.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.