Skip to content

Commit

Permalink
Fixed issues in BulkCopy exception handling (#801)
Browse files Browse the repository at this point in the history
1. Removing the catch block, because the driver should not blindly send attention packets. To cancel a query, attention packets should only be sent if the client has already sent a request, which has the last packet with EOM bit (0x01) set in status. If a complete request has not been sent to the server, the driver MUST send the next packet with both ignore bit (0x02) and EOM bit (0x01) set in the status to cancel the request (2.2.1.7 of TDS Technical Documentation). This is already properly handled in IOBuffer.execute(), which is the only method that calls doInsertBulk().
2. Adding this.tdsMessageType == TDS.PKT_BULK, because prior to TDS.PKT_BULK, the driver has already sent INSERT BULK SQL statement to the server, which in case of an exception needs to be cancelled too.
  • Loading branch information
ulvii authored Sep 24, 2018
1 parent 550d441 commit ca7141f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3132,7 +3132,7 @@ final void endMessage() throws SQLServerException {
// the client MUST send the next packet with both ignore bit (0x02) and EOM bit (0x01)
// set in the status to cancel the request.
final boolean ignoreMessage() throws SQLServerException {
if (packetNum > 0) {
if (packetNum > 0 || TDS.PKT_BULK == this.tdsMessageType) {
assert !isEOMSent;

if (logger.isLoggable(Level.FINER))
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -1559,22 +1559,6 @@ private boolean doInsertBulk(TDSCommand command) throws SQLServerException {
} finally {
tdsWriter = command.getTDSWriter();
}
} catch (SQLServerException ex) {
if (null == tdsWriter) {
tdsWriter = command.getTDSWriter();
}

// Close the TDS packet before handling the exception
writePacketDataDone(tdsWriter);

// Send Attention packet to interrupt a complete request that was already sent to the server
command.startRequest(TDS.PKT_CANCEL_REQ);

TDSParser.parse(command.startResponse(), command.getLogContext());
command.interrupt(ex.getMessage());
command.onRequestComplete();

throw ex;
} finally {
if (null == tdsWriter) {
tdsWriter = command.getTDSWriter();
Expand Down

0 comments on commit ca7141f

Please sign in to comment.