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 46d48b5dc928..1f3f76063185 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 ed77488e1949..7e80977af634 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 @@ -105,21 +105,85 @@ public void teardownServer() public void testCommit() throws SQLException { - try (Connection connection = createConnection()) { - connection.setAutoCommit(false); - try (Statement statement = connection.createStatement()) { - statement.execute("CREATE TABLE test_commit (x bigint)"); + try { + 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.commit(); } - try (Connection otherConnection = createConnection()) { - assertThat(listTables(otherConnection)).doesNotContain("test_commit"); + try (Connection connection = createConnection()) { + assertThat(listTables(connection)).contains("test_commit"); + } + } + finally { + try (Connection connection = createConnection()) { + try (Statement statement = connection.createStatement()) { + statement.execute("DROP TABLE test_commit"); + } + } + } + } + + @Test + public void testAutoCommit() + throws SQLException + { + try { + try (Connection connection = createConnection()) { + connection.setAutoCommit(true); + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE test_autocommit (x bigint)"); + } } - connection.commit(); + try (Connection connection = createConnection()) { + assertThat(listTables(connection)).contains("test_autocommit"); + } + } + finally { + try (Connection connection = createConnection()) { + try (Statement statement = connection.createStatement()) { + statement.execute("DROP TABLE test_autocommit"); + } + } } + } - try (Connection connection = createConnection()) { - assertThat(listTables(connection)).contains("test_commit"); + @Test + public void testResetAutoCommit() + throws SQLException + { + try { + try (Connection connection = createConnection()) { + connection.setAutoCommit(false); + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE test_reset_autocommit (x bigint)"); + } + + try (Connection otherConnection = createConnection()) { + assertThat(listTables(otherConnection)).doesNotContain("test_reset_autocommit"); + } + connection.setAutoCommit(true); + } + + try (Connection connection = createConnection()) { + assertThat(listTables(connection)).contains("test_reset_autocommit"); + } + } + finally { + try (Connection connection = createConnection()) { + try (Statement statement = connection.createStatement()) { + statement.execute("DROP TABLE test_reset_autocommit"); + } + } } }