From 796f8354fc3d954cdbf654eef9335788da7ad9dd Mon Sep 17 00:00:00 2001 From: Suraiya Hameed Date: Wed, 10 May 2017 15:42:46 -0700 Subject: [PATCH] Handle BulkCopy Exceptions --- .../sqlserver/jdbc/SQLServerBulkCopy.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java index dcaeee8a0..725fbfac7 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java @@ -1809,11 +1809,11 @@ private void getSourceMetadata() throws SQLServerException { } else if (null != sourceBulkRecord) { Set columnOrdinals = sourceBulkRecord.getColumnOrdinals(); - srcColumnCount = columnOrdinals.size(); - if (0 == srcColumnCount) { + if (null == columnOrdinals || 0 == columnOrdinals.size()) { throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveColMeta"), null); } else { + srcColumnCount = columnOrdinals.size(); Iterator columnsIterator = columnOrdinals.iterator(); while (columnsIterator.hasNext()) { currentColumn = columnsIterator.next(); @@ -3257,7 +3257,15 @@ private boolean writeBatchData(TDSWriter tdsWriter, // Copy from a file. else { // Get all the column values of the current row. - Object[] rowObjects = sourceBulkRecord.getRowData(); + Object[] rowObjects; + + try { + rowObjects = sourceBulkRecord.getRowData(); + } + catch (Exception ex) { + // if no more data available to retrive + throw new SQLServerException(SQLServerException.getErrString("R_unableRetrieveSourceData"), ex); + } for (int i = 0; i < mappingColumnCount; ++i) { // If the SQLServerBulkCSVRecord does not have metadata for columns, it returns strings in the object array.