-->
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.  [ 4 posts ] 
Author Message
 Post subject: Is CriteriaQuery reusable and thread-safe?
PostPosted: Thu Feb 10, 2011 2:22 pm 
Beginner
Beginner

Joined: Thu Oct 06, 2005 7:34 pm
Posts: 20
Using the JPA2 CriteriaQuery API I'd like to just build the CriteriaQuery objects once for my DAO in an initialization method and then call EntityManager.createQuery(CriteriaQuery) each time I need to actually execute the query. From my testing this appears to work just fine but the docs and spec say nothing about CriteriaQuery's re-usability or thread safety. Any thoughts here?

Also is there any appreciable different in the amount of runtime HQL/SQL generation work that Hibernate has to do when using CriteriaQuery versus @NamedQuery? It seems like it is a reasonable thought to want to avoid re-parsing the query structure every time the DAO method is called.


Top
 Profile  
 
 Post subject: Re: Is CriteriaQuery reusable and thread-safe?
PostPosted: Sun Feb 13, 2011 9:52 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
This is really an interesting question, I would not expect any of the criteria classes to be thread-safe since there is nothing mentioned in the specification about this. At least you create the CriteriaBuilder using the persistence context which is definitely not thread-safe!
Hibernate has it's own DetachedCriteria for this area and I think this is simply not covered in the current JPA spec (yet).

regards,

Robin.


Top
 Profile  
 
 Post subject: Re: Is CriteriaQuery reusable and thread-safe?
PostPosted: Sun Feb 13, 2011 10:03 am 
Beginner
Beginner

Joined: Thu Oct 06, 2005 7:34 pm
Posts: 20
Well you can also create them from a CriteriaBuilder on the EntityManagerFactory which is thread-safe. Looking at the code (and doing some basic testing) indicates that this approach does work. There doesn't appear to be any state stored in the CriteriaQuery other than a reference to the EntityManagerFactory which makes me think this is ok.

It would be even better if JPA allowed for pre-compilatioon of the queries as well. One of the nice things about he @NamedQuery annotations is Hibernate does all the JPQL/HQL parsing and SQL generation during initialization so there is never a run-time cost.


Top
 Profile  
 
 Post subject: Re: Is CriteriaQuery reusable and thread-safe?
PostPosted: Thu Mar 05, 2015 8:21 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
The CDI 1.1 specification shows an example which does re-use a CriteriaQuery (section 1.3.1, JSF example) but the Spring Data guys had to synchronize access to a shared CriteriaQuery, see https://jira.spring.io/browse/DATAJPA-396.
So it seems that it's currently not safe to re-use a CriteriaQuery without proper synchronization. It's a pity that the JPA spec isn't clearer in this case.


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