pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #10847: Connection.setSavepoint()/releaseSavepoint() is not thread-safe
3+ messages / 3 participants
[nested] [flat]

* BUG #10847: Connection.setSavepoint()/releaseSavepoint() is not thread-safe
@ 2014-07-03 07:53  christian@schlichtherle.de
  0 siblings, 1 reply; 3+ messages in thread

From: christian@schlichtherle.de @ 2014-07-03 07:53 UTC (permalink / raw)
  To: pgsql-bugs

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:

<pre><code>
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();
        },
    }
}
</code></pre>

When run, this code frequently outputs the following stack trace:

<pre><code>
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)
</code></pre>



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #10847: Connection.setSavepoint()/releaseSavepoint() is not thread-safe
@ 2014-07-03 18:54  Greg Stark <stark@mit.edu>
  parent: christian@schlichtherle.de
  0 siblings, 1 reply; 3+ messages in thread

From: Greg Stark @ 2014-07-03 18:54 UTC (permalink / raw)
  To: christian@schlichtherle.de; +Cc: pgsql-bugs

I think you need to report this to the JDBC mailing list at
pgsql-jdbc@postgresql.org

http://www.postgresql.org/list/pgsql-jdbc/

The Postgres libpq interface is most definitely not threadsafe but I
don't see any indication in that stack trace that it's using libpq. It
seems to be pure Java there.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #10847: Connection.setSavepoint()/releaseSavepoint() is not thread-safe
@ 2014-07-03 21:31  John R Pierce <pierce@hogranch.com>
  parent: Greg Stark <stark@mit.edu>
  0 siblings, 0 replies; 3+ messages in thread

From: John R Pierce @ 2014-07-03 21:31 UTC (permalink / raw)
  To: pgsql-bugs

On 7/3/2014 11:54 AM, Greg Stark wrote:
> I think you need to report this to the JDBC mailing list at
> pgsql-jdbc@postgresql.org
>
> http://www.postgresql.org/list/pgsql-jdbc/
>
> The Postgres libpq interface is most definitely not threadsafe but I
> don't see any indication in that stack trace that it's using libpq. It
> seems to be pure Java there.

yes, the jdbc driver is pure java.  you still should avoid using a jdbc 
sql connection from more than one thread on the same transaction.  its 
fine to reuse a connection between transactions as long as only one 
thread is using it at a time.



-- 
john r pierce                                      37N 122W
somewhere on the middle of the left coast



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-07-03 21:31 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-07-03 07:53 BUG #10847: Connection.setSavepoint()/releaseSavepoint() is not thread-safe christian@schlichtherle.de
2014-07-03 18:54 ` Greg Stark <stark@mit.edu>
2014-07-03 21:31   ` John R Pierce <pierce@hogranch.com>

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