diff --git a/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoConnection.java b/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoConnection.java index 46d48b5dc928f..1f3f760631850 100644 --- a/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoConnection.java +++ b/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoConnection.java @@ -187,10 +187,11 @@ public void setAutoCommit(boolean autoCommit) throws SQLException { checkOpen(); - boolean wasAutoCommit = this.autoCommit.getAndSet(autoCommit); + boolean wasAutoCommit = this.autoCommit.get(); if (autoCommit && !wasAutoCommit) { commit(); } + this.autoCommit.set(autoCommit); } @Override diff --git a/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java b/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java index ed77488e19498..073b000db7873 100644 --- a/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java +++ b/presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestJdbcConnection.java @@ -123,6 +123,43 @@ public void testCommit() } } + @Test + public void testAutoCommit() + throws SQLException + { + try (Connection connection = createConnection()) { + connection.setAutoCommit(true); + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE test_commit (x bigint)"); + } + } + + try (Connection connection = createConnection()) { + assertThat(listTables(connection)).contains("test_commit"); + } + } + + @Test + public void testResetAutoCommit() + throws SQLException + { + try (Connection connection = createConnection()) { + connection.setAutoCommit(false); + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE test_commit (x bigint)"); + } + + try (Connection otherConnection = createConnection()) { + assertThat(listTables(otherConnection)).doesNotContain("test_commit"); + } + connection.setAutoCommit(true); + } + + try (Connection connection = createConnection()) { + assertThat(listTables(connection)).contains("test_commit"); + } + } + @Test public void testRollback() throws SQLException