-->
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: How make Hibernate Interceptor Thread-Safe???
PostPosted: Wed Dec 28, 2011 8:11 pm 
Newbie

Joined: Wed Dec 28, 2011 8:07 pm
Posts: 1
Hi, just wondering how to make the following interceptor thread-safe. Any idea or suggestions?
The interceptor is global scoped, not session-scoped.

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  
 
 Post subject: Re: How make Hibernate Interceptor Thread-Safe???
PostPosted: Wed Apr 04, 2012 4:10 am 
Newbie

Joined: Wed Jan 30, 2008 10:04 am
Posts: 2
Hello,

I've got the same problem.
How to use an interceptor openning a session in a THREAD mode????

Any idea????


Top
 Profile  
 
 Post subject: Re: How make Hibernate Interceptor Thread-Safe???
PostPosted: Wed Apr 04, 2012 8:49 am 
Newbie

Joined: Wed Apr 04, 2012 8:45 am
Posts: 1
Location: London
Hello,

This is interesting. I have a friend who actually used some kind of a software to manage this codes better. I don`t know what type of services you are using.

Best,

_________________
inventory clerk
landlord inventory London


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.