Skip to content

Commit 03ce6d7

Browse files
committed
Remove the setBulkCopyOptions method from PreparedStatement
-Since the change has added connection string options for setting various bulk copy options, this non-standard API is not needed.
1 parent 193b9a2 commit 03ce6d7

File tree

2 files changed

+2
-129
lines changed

2 files changed

+2
-129
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

+2-27
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ private void setPreparedStatementHandle(int handle) {
133133
*/
134134
private SQLServerBulkCopy bcOperation = null;
135135

136-
/**
137-
* Bulk Copy Options to be used for bulkcopy
138-
*/
139-
private SQLServerBulkCopyOptions bcOptions = null;
140-
141136
/**
142137
* Bulkcopy operation table name
143138
*/
@@ -186,16 +181,6 @@ private void setUseBulkCopyForBatchInsert(boolean useBulkCopyForBatchInsert) thr
186181
this.useBulkCopyForBatchInsert = useBulkCopyForBatchInsert;
187182
}
188183

189-
/**
190-
* Sets SQLServerBulkCopyOptions in preapred statement.
191-
*
192-
* @param options
193-
* the user supplied SQLServerBulkCopyOptions value
194-
*/
195-
public void setBulkCopyOptions(SQLServerBulkCopyOptions options) {
196-
this.bcOptions = options;
197-
}
198-
199184
@Override
200185
public int getPreparedStatementHandle() throws SQLServerException {
201186
checkClosed();
@@ -2239,12 +2224,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
22392224

22402225
if (null == bcOperation) {
22412226
bcOperation = new SQLServerBulkCopy(connection);
2242-
SQLServerBulkCopyOptions option = null;
2243-
if (this.bcOptions == null) {
2244-
option = new SQLServerBulkCopyOptions(connection);
2245-
} else {
2246-
option = this.bcOptions;
2247-
}
2227+
SQLServerBulkCopyOptions option = new SQLServerBulkCopyOptions(connection);
22482228
option.setBulkCopyTimeout(queryTimeout);
22492229
bcOperation.setBulkCopyOptions(option);
22502230
bcOperation.setDestinationTableName(bcOperationTableName);
@@ -2425,12 +2405,7 @@ public long[] executeLargeBatch() throws SQLServerException, BatchUpdateExceptio
24252405

24262406
if (null == bcOperation) {
24272407
bcOperation = new SQLServerBulkCopy(connection);
2428-
SQLServerBulkCopyOptions option = null;
2429-
if (this.bcOptions == null) {
2430-
option = new SQLServerBulkCopyOptions(connection);
2431-
} else {
2432-
option = this.bcOptions;
2433-
}
2408+
SQLServerBulkCopyOptions option = new SQLServerBulkCopyOptions(connection);
24342409
option.setBulkCopyTimeout(queryTimeout);
24352410
bcOperation.setBulkCopyOptions(option);
24362411
bcOperation.setDestinationTableName(bcOperationTableName);

src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBCOptionsTest.java

-102
Original file line numberDiff line numberDiff line change
@@ -320,108 +320,6 @@ public void testBulkInsertNoOptions() throws Exception {
320320
}
321321
}
322322

323-
/**
324-
* Test with useBulkCopyBatchInsert=true passing SQLServerBulkCopyOptions with
325-
* constraint check enabled
326-
*
327-
* @throws SQLException
328-
*/
329-
@Test
330-
public void testBulkInsertWithConstraintCheckEnabled() throws Exception {
331-
// Set BulkCopy options
332-
SQLServerBulkCopyOptions options = new SQLServerBulkCopyOptions();
333-
// options.setKeepIdentity(true); // Preserve identity values from the source
334-
options.setCheckConstraints(true); // enable constraint checks
335-
// options.setTableLock(true); // Lock the destination table for faster insert
336-
// options.setBatchSize(1000); // Batch size for the bulk copy
337-
// options.setTimeout(60); // Timeout in seconds
338-
339-
try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;")) {
340-
try (PreparedStatement pstmt = connection.prepareStatement("insert into " + tableName + " values(?, ?)")) {
341-
342-
((SQLServerPreparedStatement) pstmt).setBulkCopyOptions(options);
343-
344-
pstmt.setInt(1, 1);
345-
pstmt.setInt(2, 0);
346-
pstmt.addBatch();
347-
348-
pstmt.setInt(1, 2);
349-
pstmt.setInt(2, 2);
350-
pstmt.addBatch();
351-
352-
pstmt.setInt(1, 3);
353-
pstmt.setInt(2, 0);
354-
pstmt.addBatch();
355-
356-
pstmt.setInt(1, 4);
357-
pstmt.setInt(2, 4);
358-
pstmt.addBatch();
359-
360-
pstmt.executeBatch();
361-
362-
fail(TestResource.getResource("R_expectedExceptionNotThrown"));
363-
364-
}
365-
} catch (SQLException e) {
366-
if (!e.getMessage().contains("CHECK")) {
367-
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
368-
}
369-
}
370-
}
371-
372-
/**
373-
* Test with useBulkCopyBatchInsert=true passing SQLServerBulkCopyOptions with
374-
* constraint check disabled
375-
*
376-
* @throws SQLException
377-
*/
378-
@Test
379-
public void testBulkInsertWithConstraintCheckDisabled() throws Exception {
380-
// Set BulkCopy options
381-
SQLServerBulkCopyOptions options = new SQLServerBulkCopyOptions();
382-
// options.setKeepIdentity(true); // Preserve identity values from the source
383-
options.setCheckConstraints(false); // enable constraint checks
384-
// options.setTableLock(true); // Lock the destination table for faster insert
385-
// options.setBatchSize(1000); // Batch size for the bulk copy
386-
// options.setTimeout(60); // Timeout in seconds
387-
388-
try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;")) {
389-
try (PreparedStatement pstmt = connection.prepareStatement("insert into " + tableName + " values(?, ?)")) {
390-
391-
((SQLServerPreparedStatement) pstmt).setBulkCopyOptions(options);
392-
393-
pstmt.setInt(1, 1);
394-
pstmt.setInt(2, 0);
395-
pstmt.addBatch();
396-
397-
pstmt.setInt(1, 2);
398-
pstmt.setInt(2, 2);
399-
pstmt.addBatch();
400-
401-
pstmt.setInt(1, 3);
402-
pstmt.setInt(2, 0);
403-
pstmt.addBatch();
404-
405-
pstmt.setInt(1, 4);
406-
pstmt.setInt(2, 4);
407-
pstmt.addBatch();
408-
409-
pstmt.executeBatch();
410-
411-
try (Statement stmt = connection.createStatement()) {
412-
try (ResultSet rs = stmt.executeQuery("select count(*) from " + tableName)) {
413-
if (rs.next()) {
414-
int cnt = rs.getInt(1);
415-
assertEquals(cnt, 4, "row count should have been 4");
416-
}
417-
}
418-
}
419-
}
420-
} catch (SQLException e) {
421-
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
422-
}
423-
}
424-
425323
/**
426324
* Test with useBulkCopyBatchInsert=true and
427325
* bulkCopyOptionDefaultsTableLock=true

0 commit comments

Comments
 (0)