Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lucene QueryParser in multiple threads: synchronize or construct new each time?

I have a web application where users submit queries to a Lucene index. The queries are parsed by a Lucene QueryParser. I learned the hard way that QueryParser is not thread-safe.

Is it better to use a single QueryParser instance, and synchronize on calls to its parse() method? Or is it better to construct a new instance for each query? (Or would I be better served by a pool of QueryParsers?)

I know that in general questions like this depend on the particulars and require profiling, but maybe someone out there can say definitively "QueryParsers are extremely inexpensive/expensive to construct"?

like image 895
Robert Tupelo-Schneck Avatar asked Apr 02 '11 18:04

Robert Tupelo-Schneck


1 Answers

Create a new one each time. These are lightweight objects and the JVM handles object creation and garbage collection very well. Definitely do not use an object pool.

like image 112
Joel Avatar answered Oct 23 '22 04:10

Joel