-->
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: Hibernate Validator and performance
PostPosted: Thu Sep 20, 2012 9:07 am 
Newbie

Joined: Thu Sep 20, 2012 8:38 am
Posts: 2
Hi,

I'm new to a system which has performance problems.
We are using Hibernate with JPA 1.0.
I just noticed we use Hibernate Validation (version 4.0.0.GA).
When googling I found a thread on this forum which had some information regarding Hibernate Validation and
performance. One thing stated in that thread was:
Quote:
Most important performance recommendation is to not throw away the ValidatorFactory. The cache works per factory. Validator instances are lightweight and can be easily re-created.

We have specified a Validation class of our own in persistence.xml like this:

Code:
<property name="javax.persistence.validation.group.pre-persist" value="se.ourcompany.dao.validation.BeanValidationEventListener"/>
<property name="javax.persistence.validation.group.pre-update" value="se.ourcompany.dao.validation.BeanValidationEventListener"/>
<property name="javax.persistence.validation.group.pre-remove" value=""/>


And code from BeanValidationEventListener:


Code:
public class BeanValidationEventListener {
   private Log log = LogFactory.getLog(BeanValidationEventListener.class);

   @PrePersist
   @PreUpdate
   @PreRemove
   public void validate(Object entity) {
      TraversableResolver tr = new BeanTraversableResolver();
      Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
      final Set<ConstraintViolation<Object>> constraintViolations = validator.validate(entity);
      if (constraintViolations.size() > 0) {
           //some logging takes place         
      }
   }
}

Our entities do have a lot of validated fields and a complicated design with many associations.
Should we re-use our validation factory instead - instantiating it as a class member?
Like so:
Code:
..
private ValidatorContext validatorContext = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(new BeanTraversableResolver());
..

And then in the validate method:
Code:
..
Validator validator = validatorContext.getValidator();
..


When running one of our JUnit database test classes I do notice the new code is significantly faster.

/best regards, Håkan


Top
 Profile  
 
 Post subject: Re: Hibernate Validator and performance
PostPosted: Fri Sep 21, 2012 4:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Håkan,

I can see several potential problems. First off, yes you should reuse ValidatorFactory. How exactly will depend on your framework. Best would be to reuse the ValidatorFactory provided by the container (in case you are using one. You are using JPA so I thought you probably do). If you don't reuse the factory you won't be able to benefit of the caching of metadata which is handled in the factory. I am not sure whether using the listener class is the best place to do this sort of caching though.

Another thing I noticed is:

Quote:
<property name="javax.persistence.validation.group.pre-persist" value="se.ourcompany.dao.validation.BeanValidationEventListener"/>
<property name="javax.persistence.validation.group.pre-update" value="se.ourcompany.dao.validation.BeanValidationEventListener"/>
<property name="javax.persistence.validation.group.pre-remove" value=""/>


This does not to anything. javax.persistence.validation.group has a completely different purpose. It specified the groups to be validated when automatic JPA life cycle event validation is enabled. I would expect you should be able to remove this whole section without any change in behavior. It is just wrong and misleading.

Last, but not least - you should upgrade to the latest Hibernate Validator 4.3 release. 4.0.0.GA is a very old release and upgrading would give you a ton of bug fixes and performance improvements.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Validator and performance
PostPosted: Mon Sep 24, 2012 3:46 am 
Newbie

Joined: Thu Sep 20, 2012 8:38 am
Posts: 2
Hardy,

Thank you so much for a clarifying answer.

/Håkan


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.