Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X2jou-0001T5-0z for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Jul 2014 16:25:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X2jos-0002Nu-Pt for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Jul 2014 16:25:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X2bqI-000898-Qn for pgsql-bugs@postgresql.org; Thu, 03 Jul 2014 07:54:06 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X2bqF-00017o-IR for pgsql-bugs@postgresql.org; Thu, 03 Jul 2014 07:54:05 +0000 MIME-Version: 1.0 Subject: BUG #10847: Connection.setSavepoint()/releaseSavepoint() is not thread-safe To: pgsql-bugs@postgresql.org From: christian@schlichtherle.de Date: Thu, 03 Jul 2014 07:53:43 +0000 Message-ID: <20140703075343.5122.97077@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 10847 Logged by: Christian Schlichtherle Email address: christian@schlichtherle.de PostgreSQL version: 9.3.4 Operating system: OS X 10.9.3 Description: I am not sure if this is a bug or a feature. One might argue that you shouldn't share a connection between threads. Anyway, following is a standalone test to reproduce the issue in PostgreSQL 9.3.4 using the JDBC driver version 9.3-1101-jdbc41:

package cpssd.db;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.concurrent.CountDownLatch;

/** @author Christian Schlichtherle */
public class Ticket251IT {

    private static final String CONNECTION_STRING =
"jdbc:postgresql:postgres";
    private static final int NUM_THREADS = 2;

    @Test public void foo() throws SQLException, InterruptedException {
        try (Connection c = DriverManager.getConnection(CONNECTION_STRING))
{
            c.setAutoCommit(false);
            final Runnable task = new Runnable() {
                final CountDownLatch startSignal = new
CountDownLatch(NUM_THREADS);

                @Override public void run() {
                    try {
                        startSignal.countDown();
                        startSignal.await();
                        // FIXME: This idiom doesn't work on a shared
connection!
                        Savepoint sp = c.setSavepoint();
                        try {
                            // Insert transaction script here...
                        } finally {
                            c.releaseSavepoint(sp);
                        }
                    } catch (SQLException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            final Thread[] threads = new Thread[NUM_THREADS];
            for (int i = 0; i < threads.length; i++)
                (threads[i] = new Thread(task)).start();
            for (Thread thread : threads)
                thread.join();
        },
    }
}
When run, this code frequently outputs the following stack trace:

org.postgresql.util.PSQLException: ERROR: no such savepoint
	at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
	at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
	at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:559)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)
	at
org.postgresql.jdbc2.AbstractJdbc2Connection.execSQLUpdate(AbstractJdbc2Connection.java:376)
	at
org.postgresql.jdbc3.AbstractJdbc3Connection.releaseSavepoint(AbstractJdbc3Connection.java:192)
	at cpssd.db.Ticket251IT$1.run(Ticket251IT.java:32)
	at java.lang.Thread.run(Thread.java:745)
-- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs