Skip to content

Commit

Permalink
Fix failure with enabling JDBC autocommit
Browse files Browse the repository at this point in the history
There was a bug where enabling autocommit when it had previously been
false would cause failure like:
java.sql.SQLException: Connection is in auto-commit mode

This was due to calling commit() from setAutoCommit() after autocommit
had already been changed to true.

Fixes prestodb#15916
  • Loading branch information
rschlussel committed Aug 15, 2024
1 parent 330fa47 commit 1d94a47
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1d94a47

Please sign in to comment.