Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure with enabling JDBC autocommit #23453

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +105,7 @@ public void teardownServer()
public void testCommit()
throws SQLException
{
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to see in github, but the only thing that's changed about this test is that it's wrapped in an extra try so I could put a finally at the end to drop the table.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A neat trick is to take the url in the browser and add the query parameter ?w=1, which will cause whitespaces to be ignored. e.g. https://github.com/prestodb/presto/pull/23453/files?w=1

try (Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (Statement statement = connection.createStatement()) {
Expand All @@ -122,6 +123,69 @@ public void testCommit()
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)");
}
}

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");
}
}
}
}

@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");
}
}
}
}

@Test
public void testRollback()
Expand Down
Loading