Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 6D13F9FA4B5 for ; Thu, 4 Jan 2007 14:24:33 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 74984-07 for ; Thu, 4 Jan 2007 14:24:27 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail63.csoft.net (mail63.csoft.net [205.205.214.4]) by postgresql.org (Postfix) with ESMTP id A3E029FA38F for ; Thu, 4 Jan 2007 14:24:27 -0400 (AST) Received: by mail63.csoft.net (Postfix, from userid 1112) id 0FD9BF2C8; Thu, 4 Jan 2007 13:24:27 -0500 (EST) Date: Thu, 4 Jan 2007 13:24:27 -0500 (EST) From: Kris Jurka X-X-Sender: books@leary2.csoft.net To: Morten Andersen cc: pgsql-jdbc@postgresql.org Subject: Re: Unexpected NullPointerException in "processDeadParsedQueries()" In-Reply-To: <4589297C.208@instadia.net> Message-ID: References: <4589297C.208@instadia.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/22 X-Sequence-Number: 16387 On Wed, 20 Dec 2006, Morten Andersen wrote: > In a multithreaded application I just got an unexpected NullPointerException > from the depth's of the JDBC-drivers cleanup routine of parsed queries. > Is this something you can reproduce somewhat regularly or something that just happened once? > During a batch-execution of a trivial PreparedStatement that generally works > fine I got the NullPointerException below: > > Exception in thread "Thread-5" java.lang.NullPointerException > at org.postgresql.core.Utils.encodeUTF8(Utils.java:54) > at > org.postgresql.core.v3.QueryExecutorImpl.sendCloseStatement(QueryExecutorImpl.java:982) > at > org.postgresql.core.v3.QueryExecutorImpl.processDeadParsedQueries(QueryExecutorImpl.java:1104) > at > > I have been browsing the CVS-repository and I think the code that generates > this is the processDeadParsedQueries() below: > > private void processDeadParsedQueries() throws IOException { > PhantomReference deadQuery; > while ((deadQuery = (PhantomReference)parsedQueryCleanupQueue.poll()) != > null) > { > String statementName = (String)parsedQueryMap.remove(deadQuery); <--- > RETURNS null? > sendCloseStatement(statementName); <--- TRIGGERS NullPointerException > deadQuery.clear(); > } > } > > I inserted the comments with "<---". > > The reaon why it is possible for the parsedQueryMap to return a null > statementName for the deadQuery key is quite a puzzle to me. Mostly I can > only think of two reasons: > > 1) The statementName for the deadQuery has already been removed. Given the > code for registerParsedQuery() and processDeadParsedQueries() I can't see how > this should be possible, as the java.lang.ref.ReferenceQueue is thread-safe > (it doesn't say so in the API documentation, but that is what you would > expect, and a quick glance at the JDK source also indicates that this is the > case). > > 2) The use of HashMap for the parsedQueryMap could be a problem in a > multithreaded application, since it is not thread-safe? So a wild guess is > that the thread executing the processDeadParsedQueries() does not "see" the > value that was added to the hashmap by another thread in the > registerParsedQuery. > Neither of these look particularly likely to me. All entry points to QueryExecutorImpl that access these items are synchronized so I don't think the thread safety of the individual items are an issue. Is your application multi-threaded and using a Connection per thread or does it use multiple threads per connection? If using Connection per thread it's even harder to believe this is a thread safety issue because you'll have a QueryExecutorImpl per Connection. Kris Jurka