diff --git a/src/main/java/com/amazon/redshift/jdbc/RedshiftStatementImpl.java b/src/main/java/com/amazon/redshift/jdbc/RedshiftStatementImpl.java index a9069fa..3c01e88 100644 --- a/src/main/java/com/amazon/redshift/jdbc/RedshiftStatementImpl.java +++ b/src/main/java/com/amazon/redshift/jdbc/RedshiftStatementImpl.java @@ -741,6 +741,15 @@ public ResultSet getResultSet() throws SQLException { */ public final void close() throws SQLException { + if (connection.getQueryExecutor().isRingBufferThreadRunning() && + (!connection.getAutoCommit() || getMaxRows() == 0)) + { + // Wait for current ring buffer thread to finish, if any. + // Shouldn't call from synchronized method, which can cause dead-lock. + // We don't want to wait for ring buffer to fetch all rows if setMaxRows() is set, + // or if autoCommit is set to true + connection.getQueryExecutor().waitForRingBufferThreadToFinish(false, false, true, null, null); + } if (RedshiftLogger.isEnable()) connection.getLogger().logFunction(true);