-->
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: JPAValidateListener and Thread Safety
PostPosted: Thu Jan 22, 2009 6:31 pm 
Newbie

Joined: Thu Jan 22, 2009 6:22 pm
Posts: 5
Hibernate version: 3.3.1

Hi,

I was looking at the code for JPAValidateListener to see how it cached ClassValidators in a servlet environment. However, it seems to use an unsynchronized static map to cache them. How does this class avoid thread safety issues when creating/accessing the ClassValidators?

Any general recommendations for caching ClassValidators in a Spring/Hibernate/Servlet environment?

Here is the code for reference:

Code:
public class JPAValidateListener {
   //No need for weakReference if the event listener is loaded by the same CL than
   //the entities, but in a shared env this is not the case
   //TODO check that this can be hot redeployable
   private static final Map<Class, WeakReference<ClassValidator>> validators;
   private static final ClassValidator<JPAValidateListener> NO_VALIDATOR;
   static {
      validators = new WeakHashMap<Class, WeakReference<ClassValidator>>();
      NO_VALIDATOR = new ClassValidator<JPAValidateListener>( JPAValidateListener.class);
   }

   //keep hardref at the instance level
   private final Set<ClassValidator> currentValidators = new HashSet<ClassValidator>();

   @PrePersist
   @PreUpdate
   @SuppressWarnings( "unchecked" )
   public void onChange(Object object) {
      if (object == null) return;
      Class entity = object.getClass();
      WeakReference<ClassValidator> weakValidator = validators.get(entity);
      ClassValidator validator = weakValidator != null ? weakValidator.get() : null;
      if ( validator == null ) {
         //initialize
         //TODO reuse the same reflection manager?
         validator = new ClassValidator(entity);
         if ( ! validator.hasValidationRules() ) {
            validator = NO_VALIDATOR;
         }
         validators.put( entity, new WeakReference<ClassValidator>(validator) );
         currentValidators.add( validator );
      }
      if ( validator != NO_VALIDATOR ) {
         validator.assertValid( object );
      }
   }
}


Thank you for your help!


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.