-->
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.  [ 1 post ] 
Author Message
 Post subject: How make Hibernate Interceptor Thread-Safe???
PostPosted: Fri Aug 07, 2009 1:11 am 
Newbie

Joined: Thu Feb 12, 2009 10:04 pm
Posts: 1
Hi, just wondering how to make the following interceptor thread-safe without sacrifice on performance in real application?

And when postFlush() being called, can I assume the transaction has been successfully submitted? And I guess when onFlushDirty() being called I can't start to write the audit log yet as the transaction may be rolled back. Am I right?

Thank you so much.

Code:
public class AuditLogInterceptor extends EmptyInterceptor {
  private Session session;
  private Long userId;
  private Set inserts = new HashSet();
  private Set updates = new HashSet();
  public void setSession(Session session) {
    this.session=session;
  }
  public void setUserId(Long userId) {
    this.userId=userId;
  }
  public boolean onSave(Object entity,
    Serializable id,
    Object[] state,
    String[] propertyNames,
    Type[] types)
    throws CallbackException {
      if (entity instanceof Auditable)
        inserts.add(entity);
        return false;
      }
  public boolean onFlushDirty(Object entity,
    Serializable id,
    Object[] currentState,
    Object[] previousState,
    String[] propertyNames,
    Type[] types)
    throws CallbackException {
      if (entity instanceof Auditable)
        updates.add(entity);
       return false;
  }
  public void postFlush(Iterator iterator)
    throws CallbackException {
    try {
      for (Iterator it = inserts.iterator(); it.hasNext();) {
         Auditable entity = (Auditable) it.next();
         AuditLog.logEvent("create",entity,userId,session.connection());
      }
      for (Iterator it = updates.iterator(); it.hasNext();) {
         Auditable entity = (Auditable) it.next();
         AuditLog.logEvent("update",entity,userId,session.connection());
      }
    } finally {
      inserts.clear();
      updates.clear();
  }
}
}


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

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.