Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A7B14632EAE for ; Mon, 13 Apr 2009 14:49:05 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 07128-02 for ; Mon, 13 Apr 2009 14:48:56 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gwmta.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by mail.postgresql.org (Postfix) with ESMTP id 66467632BBA for ; Mon, 13 Apr 2009 14:48:56 -0300 (ADT) Received: from Courts-MTA by gwmta.wicourts.gov with Novell_GroupWise; Mon, 13 Apr 2009 12:48:55 -0500 Message-Id: <49E334AA.EE98.0025.0@wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 7.0.3 Date: Mon, 13 Apr 2009 12:48:42 -0500 From: "Kevin Grittner" To: Subject: Helping application programmers catch threading bugs Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200904/45 X-Sequence-Number: 19492 Based on this thread: http://archives.postgresql.org/pgsql-admin/2009-04/msg00100.php I was wondering whether it would make sense to add a checkThreading method to help programmers who make such mistakes catch them more easily. Unfortunately I can't think of a reliable way to do this without using synchronized blocks, but maybe someone else can. In any event, I don't think we'd want to do it if there is a significant performance hit, but I bet the cost would be "lost in the noise" of measurement. Perhaps defined something like: private Thread currentThread = null; private int useCount = 0; public synchronized void checkThreading() throws PSQLException { if (currentThread != null && currentThread != Thread.currentThread()) { throw new PSQLException(GT.tr("Multithreading error; another thread is currently using the connection."), PSQLState.?); } currentThread = Thread.currentThread(); ++useCount; } public synchronized void checkThreadingExit() throws PSQLException { if (useCount <= 0 || currentThread != Thread.currentThread()) { throw new PSQLException(GT.tr("Multithreading error; invalid state found on method exit."), PSQLState.?); } if (useCount-- == 0) { currentThread = null; } } It would be used along the lines of (for example): public java.sql.ResultSet executeQuery() throws SQLException { checkThreading(); try { } finally { checkThreadingExit(); } } Would this be a sane and worthwhile thing to do? -Kevin