Helping application programmers catch threading bugs

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Helping application programmers catch threading bugs
Date: 2009-04-13 17:48:42
Message-ID: 49E334AA.EE98.0025.0@wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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
{
<existing code here>
}
finally
{
checkThreadingExit();
}
}

Would this be a sane and worthwhile thing to do?

-Kevin

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Guy Rouillier 2009-04-13 17:51:51 Re: Can't get JDBC to compile. Please help.
Previous Message Dave Cramer 2009-04-13 15:34:47 Re: Can't get JDBC to compile. Please help.