Helping application programmers catch threading bugs - Mailing list pgsql-jdbc

From Kevin Grittner
Subject Helping application programmers catch threading bugs
Date
Msg-id 49E334AA.EE98.0025.0@wicourts.gov
Whole thread Raw
Responses Re: Helping application programmers catch threading bugs  (Kris Jurka <books@ejurka.com>)
List 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

pgsql-jdbc by date:

Previous
From: Dave Cramer
Date:
Subject: Re: Can't get JDBC to compile. Please help.
Next
From: "Kevin Grittner"
Date:
Subject: Re: Helping application programmers catch threading bugs