Return-Path: Delivered-To: apmail-lucene-java-user-archive@www.apache.org Received: (qmail 45830 invoked from network); 24 Aug 2005 09:45:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 24 Aug 2005 09:45:49 -0000 Received: (qmail 65543 invoked by uid 500); 24 Aug 2005 09:45:43 -0000 Delivered-To: apmail-lucene-java-user-archive@lucene.apache.org Received: (qmail 65523 invoked by uid 500); 24 Aug 2005 09:45:43 -0000 Mailing-List: contact java-user-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: java-user@lucene.apache.org Delivered-To: mailing list java-user@lucene.apache.org Received: (qmail 65510 invoked by uid 99); 24 Aug 2005 09:45:43 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Aug 2005 02:45:43 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [193.194.158.19] (HELO exchange.bvdep.com) (193.194.158.19) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Aug 2005 02:46:00 -0700 X-MimeOLE: Produced By Microsoft Exchange V6.0.6603.0 Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Subject: RE: QueryParser not thread-safe Date: Wed, 24 Aug 2005 11:45:40 +0200 Message-ID: <950FF7DE40C2B64CAF80B564732927E104810B62@exchange.be.bvd> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: QueryParser not thread-safe thread-index: AcWoGjStybizD+d3T46gezHgKhDRaAAb8vKg From: "Vanlerberghe, Luc" To: X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Thanks for pointing that out! I checked the source and QueryParser is indeed not thread-safe (the presence of local variables like jj_lastpos that are used *during* the parsing makes this obvious) Perhaps it should be explicitly mentioned in the javadoc. The solution I'll probably go for is using ThreadLocal variables (available since jdk 1.2, but there was a problem in the sun jvm with deallocation when threads stop until jdk 1.4 or so) an example (not compiled, nor tested): public class MyQueryFactory { public Query createQuery(String expression) { return threadLocalQueryParser.get().parse(expression); } =20 private ThreadLocal threadLocalQueryParser=3Dnew ThreadLocal() { protected QueryParser initialValue() { return new QueryParser("defaultField", new MyAnalyzer()); } }; } (I used jdk 1.5 generics because the code is clearer, remove the and put a few casts where needed if you want this in 1.4 or lower) initialValue() will be only be called once for every thread that calls threadLocalQueryParser.get(). See the javadoc for java.lang.ThreadLocal Luc -----Original Message----- From: jian chen [mailto:chenjian1227@gmail.com]=20 Sent: dinsdag 23 augustus 2005 21:38 To: java-user@lucene.apache.org Subject: Re: QueryParser not thread-safe Right. My philosophy is that, make it work, then, make it better.=20 Don't waste time on something that you are not sure if it would cause performance problem. Jian On 8/23/05, Paul Elschot wrote: > On Tuesday 23 August 2005 19:01, Miles Barr wrote: > > On Tue, 2005-08-23 at 13:47 -0300, jhandl@fibertel.com.ar wrote: > > > Hi! I've been having problems with lucene's QueryParser, apparently it is > not thread-safe. > > > > > > That means I can't parse queries in threads where the queryparser object > is created once and reused for each query. If I do, the resulting queries may > have all kinds of weird problems, for example missing terms, duplicate terms, > etc. > > > > > > I don't like the idea of creating a new queryparser for each query, so I > am looking for alternatives. Any ideas? > > > > Using a non-threadsafe object in a threaded environment is fairly > > standard in Java, just wrap it in a synchronized block. > > > > If you don't want all threads waiting on one query parser, create a pool > > of them. >=20 > Or use one parser per thread. >=20 > Regards, > Paul Elschot >=20 >=20 > --------------------------------------------------------------------- > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org > For additional commands, e-mail: java-user-help@lucene.apache.org >=20 > --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org For additional commands, e-mail: java-user-help@lucene.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org For additional commands, e-mail: java-user-help@lucene.apache.org