package com.github.davidmoten.rx.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Provides a singleton {@link Connection} sourced from a
 * {@link ConnectionProvider} that has autoCommit set to false.
 */
final class ConnectionProviderSingletonManualCommit implements ConnectionProvider {

    /**
     * Singleton connection.
     */
    private Connection con;

    /**
     * Provides the singleton connection.
     */
    private final ConnectionProvider cp;

    /**
     * Constructor.
     * 
     * @param cp
     *            connection provider.
     */
    ConnectionProviderSingletonManualCommit(ConnectionProvider cp) {
        this.cp = cp;
    }

    @Override
    public synchronized Connection get() {
        if (con == null) {
            con = cp.get();
            try {
                con.setAutoCommit(false);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return con;
    }

    @Override
    public void close() {
        cp.close();
    }

}
@@ -90,16 +90,66 @@
 ception;
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0A/**%0A *
@@ -431,16 +431,143 @@
 n con;%0A%0A
+    /**%0A     * Ensures thread-safe setting of con%0A     */%0A    private AtomicBoolean connectionSet = new AtomicBoolean(false);%0A%0A
     /**%0A
@@ -885,21 +885,8 @@
 lic 
-synchronized 
 Conn
@@ -919,16 +919,45 @@
 (con
- == null
+nectionSet.compareAndSet(false, true)
 ) %7B%0A

use compareAndSet instead of synchronized to improve concurrent performance of ConnectionProviderSingletonManualCommmit