-->
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.  [ 5 posts ] 
Author Message
 Post subject: audit of user on create or update
PostPosted: Mon Jun 13, 2005 1:39 pm 
Newbie

Joined: Thu Oct 02, 2003 5:17 am
Posts: 1
The articles I have seen for auditing have all been about providing solutions for setting the audit time. How do I go about setting the username for audit purposes?

Under Hibernate 2, I used an interceptor with a username passed in for the openSession call. Now, with Hibernate 3, I am using sessionFactory.currentSession. I cannot set a factory wide interceptor as I have no way of passing the current session username in. What is the recommended approach? Is there an alternate solution? Just setting the time is of limited value for audit purposes if we don't know who made the change.

One approach I tried was to keep a map of usernames and sessions. I then had an event listener get the username for the current session and set the audit field appropriately. This worked except for the error where a collection was not processed [strack trace below]. Not sure where to proceed from here.

Any help will be greatly appreciated.

18:01:18,815 ERROR [AbstractLavaAction]
javax.transaction.TransactionRolledbackException: null; CausedByException is:
collection was not processed by flush()
at
org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:244)
at
org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:335)
at
org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:166)
at
org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:139)
at
org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:192)
at
org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at
org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:624)
at org.jboss.ejb.Container.invoke(Container.java:873)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
at
org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
at
org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
at
org.jboss.invocation.local.LocalInvoker$MBeanServerAction.invoke(LocalInvoker.java:155)
at
org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:104)
at
org.jboss.invocation.InvokerInterceptor.invokeLocal(InvokerInterceptor.java:179)
at
org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:165)
at
org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at
org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:55)
at
org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:97)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:86)
. . . <cut> . . .


Top
 Profile  
 
 Post subject: Great Question!!!
PostPosted: Tue Jul 26, 2005 2:26 pm 
Newbie

Joined: Mon Apr 11, 2005 1:14 pm
Posts: 7
Location: Charlotte, NC
Is someone able to suggest a way to obtain a user id / name inside the event listener? This seems to be a common auditing necessity. I can't find an approach in the documentation.

Code:
   public Serializable onSaveOrUpdate(SaveOrUpdateEvent arg0)
      throws HibernateException {

      //somehow get the user id????
      
      return super.onSaveOrUpdate(arg0);
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ThreadLocal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 5:51 pm 
Newbie

Joined: Fri Mar 11, 2005 7:22 pm
Posts: 10
Were you guys able to get this up and running? I have a similar requirement.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 4:44 pm 
Newbie

Joined: Tue Mar 28, 2006 4:03 pm
Posts: 11
like Gavin soad: ThreadLocal is the solution.

Heres my solution, but you need base classes for all your session- and entitybeans:



Code:
public class PropagateSessionContextInterceptor {

 
  @AroundInvoke
  public Object myInterceptor(InvocationContext ctx) throws Exception
  {
      BaseSessionBean sb = (BaseSessionBean)ctx.getBean();
      BaseEntity.setSessionContext(sb.ctx);
      Object o = ctx.proceed();
      return o;

  }
 

}


Code:
@Interceptors( {PropagateSessionContextInterceptor.class})
public class BaseSessionBean {

  @PersistenceContext(unitName = "foobar")
  protected EntityManager manager;

  @Resource
  public SessionContext ctx;

}


Code:
@MappedSuperclass
public abstract class BaseEntity {
  static final ThreadLocal<SessionContext> sessionContext = new ThreadLocal<SessionContext>();

  static SessionContext getSessionContext() {
    return sessionContext.get();
  }

  static void setSessionContext(SessionContext ctx) {
    sessionContext.set(ctx);
  }
 
  protected long id;

  protected int version;

  protected String createdBy;

  protected String updatedBy;

  protected Date createdTime;

  protected Date updatedTime;



  public void setVersion(int version) {
    this.version = version;
  }

  @Basic
  public String getCreatedBy() {
    return createdBy;
  }

  public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
  }

  @Basic
  public String getUpdatedBy() {
    return updatedBy;
  }

  public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
  }

  @Basic
  public Date getCreatedTime() {
    return createdTime;
  }

  public void setCreatedTime(Date createdTime) {
    this.createdTime = createdTime;
  }

  @Basic
  public Date getUpdatedTime() {
    return updatedTime;
  }

  public void setUpdatedTime(Date updatedTime) {
    this.updatedTime = updatedTime;
  }



  @PrePersist
  public void handleCreate() {


    try {
      setCreatedTime(new Date());
      Principal callerPrincipal = getSessionContext().getCallerPrincipal();
      setCreatedBy(callerPrincipal == null ? null : callerPrincipal.getName());
    }
    catch (RuntimeException e) {
      e.printStackTrace();
    }
  }

  @PreUpdate
  public void handleUpdate()  {

    try {
      setUpdatedTime(new Date());
      Principal callerPrincipal = getSessionContext().getCallerPrincipal();

      setUpdatedBy(callerPrincipal == null ? null : callerPrincipal.getName());
    }
    catch (RuntimeException e) {
      e.printStackTrace();
    }
  }
 


}


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