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 #15916
  • Loading branch information
rschlussel authored and tdcmeehan committed Aug 16, 2024
1 parent 4dab583 commit bd444ec
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
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 @@ -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");
}
}
}
}

Expand Down

0 comments on commit bd444ec

Please sign in to comment.