-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: release ResultSet on Statement#close() (#1567)
* fix: release ResultSet on Statement#close() Release the underlying resources of any ResultSet when Statement#close() is called. This is necessary in case someone executes a query in auto-commit mode using Statement#execute(String), and never reads and/or closes the result that was returned. * fix: close result set when Statement is closed
- Loading branch information
Showing
3 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/com/google/cloud/spanner/jdbc/StatementResourcesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.jdbc; | ||
|
||
import com.google.cloud.spanner.connection.AbstractMockServerTest; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class StatementResourcesTest extends AbstractMockServerTest { | ||
private static final int MIN_SESSIONS = 1; | ||
private static final int MAX_SESSIONS = 2; | ||
|
||
private String createUrl() { | ||
return String.format( | ||
"jdbc:cloudspanner://localhost:%d/projects/%s/instances/%s/databases/%s" | ||
+ "?usePlainText=true;minSessions=%d;maxSessions=%d", | ||
getPort(), "proj", "inst", "db", MIN_SESSIONS, MAX_SESSIONS); | ||
} | ||
|
||
@Test | ||
public void testMultipleQueriesOnOneStatement() throws SQLException { | ||
try (Connection connection = DriverManager.getConnection(createUrl())) { | ||
try (Statement statement = connection.createStatement()) { | ||
for (int i = 0; i < MAX_SESSIONS + 1; i++) { | ||
// Execute a query without reading or closing the result. | ||
statement.execute("SELECT 1"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testMultipleStatementsWithOneQuery() throws SQLException { | ||
try (Connection connection = DriverManager.getConnection(createUrl())) { | ||
for (int i = 0; i < MAX_SESSIONS + 1; i++) { | ||
try (Statement statement = connection.createStatement()) { | ||
// Execute a query without reading or closing the result. | ||
statement.execute("SELECT 1"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testMultipleQueriesOnOnePreparedStatement() throws SQLException { | ||
try (Connection connection = DriverManager.getConnection(createUrl())) { | ||
try (PreparedStatement statement = connection.prepareStatement("SELECT 1")) { | ||
for (int i = 0; i < MAX_SESSIONS + 1; i++) { | ||
// Execute a query without reading or closing the result. | ||
statement.execute(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testMultiplePreparedStatementsWithOneQuery() throws SQLException { | ||
try (Connection connection = DriverManager.getConnection(createUrl())) { | ||
for (int i = 0; i < MAX_SESSIONS + 1; i++) { | ||
try (PreparedStatement statement = connection.prepareStatement("SELECT 1")) { | ||
// Execute a query without reading or closing the result. | ||
statement.execute(); | ||
} | ||
} | ||
} | ||
} | ||
} |