pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: pgsql-jdbc@postgresql.org
Subject: Helping application programmers catch threading bugs
Date: Mon, 13 Apr 2009 12:48:42 -0500
Message-ID: <49E334AA.EE98.0025.0@wicourts.gov> (raw)

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



view thread (3+ messages)  latest in thread

Message-ID: <49E334AA.EE98.0025.0@wicourts.gov>
Permalink:  ../49E334AA.EE98.0025.0@wicourts.gov/
Also on:    postgresql.org/message-id/49E334AA.EE98.0025.0@wicourts.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: Kevin.Grittner@wicourts.gov
  Subject: Re: Helping application programmers catch threading bugs
  In-Reply-To: <49E334AA.EE98.0025.0@wicourts.gov>
  References: <49E334AA.EE98.0025.0@wicourts.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox