Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-315] kill query when closing statement and was fetching
  • Loading branch information
rusher committed Apr 8, 2017
1 parent 7c12c61 commit 0d6e4db
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
Expand Up @@ -56,6 +56,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper;
import org.mariadb.jdbc.internal.util.dao.ServerPrepareResult;

import java.io.IOException;
import java.sql.*;
import java.util.*;

Expand Down Expand Up @@ -381,7 +382,14 @@ public void close() throws SQLException {
try {
closed = true;

if (results.getFetchSize() != 0) skipMoreResults();
if (results.getFetchSize() != 0) {
try {
protocol.cancelCurrentQuery();
skipMoreResults();
} catch (SQLException | IOException sqle) {
//eat exception
}
}
results.close();

// No possible future use for the cached results, so these can be cleared
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Expand Up @@ -676,7 +676,14 @@ public void close() throws SQLException {
try {
closed = true;

if (results.getFetchSize() != 0 && closed) skipMoreResults();
if (results.getFetchSize() != 0) {
try {
protocol.cancelCurrentQuery();
skipMoreResults();
} catch (SQLException | IOException sqle) {
//eat exception
}
}

results.close();
protocol = null;
Expand Down Expand Up @@ -843,9 +850,7 @@ public void setLocalInfileInputStream(InputStream inputStream) throws SQLExcepti
public void cancel() throws SQLException {
checkClose();
try {
if (!executing) {
return;
}
if (!executing) return;
protocol.cancelCurrentQuery();
} catch (SQLException e) {
logger.error("error cancelling query", e);
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/org/mariadb/jdbc/FetchSizeTest.java
Expand Up @@ -144,5 +144,51 @@ private void prepareRecords(int recordNumber, String tableName) throws SQLExcept
pstmt.executeBatch();
}

/**
* CONJ-315 : interrupt when closing statement.
*
* @throws SQLException sqle
*/
@Test
public void fetchSizeClose() throws SQLException {

Statement stmt = sharedConnection.createStatement();
long start = System.currentTimeMillis();
stmt.executeQuery("select * from information_schema.columns as c1, information_schema.tables");
long normalExecutionTime = System.currentTimeMillis() - start;

start = System.currentTimeMillis();
stmt.setFetchSize(1);
stmt.executeQuery("select * from information_schema.columns as c1, information_schema.tables");
stmt.close();
long interruptedExecutionTime = System.currentTimeMillis() - start;

//normalExecutionTime = 1500
//interruptedExecutionTime = 77
assertTrue("interruptedExecutionTime:" + interruptedExecutionTime
+ " normalExecutionTime:" + normalExecutionTime,
interruptedExecutionTime * 3 < normalExecutionTime);
}

@Test
public void fetchSizePrepareClose() throws SQLException {

PreparedStatement stmt = sharedConnection.prepareStatement("select * from information_schema.columns as c1, information_schema.tables");

long start = System.currentTimeMillis();
stmt.executeQuery();
long normalExecutionTime = System.currentTimeMillis() - start;

start = System.currentTimeMillis();
stmt.setFetchSize(1);
stmt.executeQuery();
stmt.close();
long interruptedExecutionTime = System.currentTimeMillis() - start;

//normalExecutionTime = 1500
//interruptedExecutionTime = 77
assertTrue("interruptedExecutionTime:" + interruptedExecutionTime
+ " normalExecutionTime:" + normalExecutionTime,
interruptedExecutionTime * 3 < normalExecutionTime);
}
}

0 comments on commit 0d6e4db

Please sign in to comment.