Skip to content

Commit

Permalink
Fix | Intermittent BulkCopyResultSetCursorTest error (#854)
Browse files Browse the repository at this point in the history
Adding unique ID to bulkcopycursor test and sorting tables by ASC before comparing to avoid intermittent insert/retrieve errors
  • Loading branch information
rene-ye authored and ulvii committed Nov 30, 2018
1 parent c3cb562 commit 12e4e49
Showing 1 changed file with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void serverCursorsTest(int resultSetType, int resultSetConcurrency) thro

try (Statement stmt2 = conn.createStatement(resultSetType, resultSetConcurrency);
ResultSet rs = stmt2
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable));
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable) + " ORDER BY id ASC");
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
bulkCopy.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy.writeToServer(rs);
Expand All @@ -96,7 +96,7 @@ public void testSelectMethodSetToCursor() throws SQLException {
createTables(stmt);
populateSourceTable();

try (ResultSet rs = stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable));
try (ResultSet rs = stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable) + " ORDER BY id ASC");
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {

bulkCopy.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
Expand All @@ -122,7 +122,7 @@ public void testMultiplePreparedStatementAndResultSet() throws SQLException {

try (Statement stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt1
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable))) {
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable) + " ORDER BY id ASC")) {
try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
bulkCopy.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy.writeToServer(rs);
Expand Down Expand Up @@ -158,7 +158,7 @@ public void testMultiplePreparedStatementAndResultSet() throws SQLException {
try (Statement stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs2 = stmt2
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable));
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable) + " ORDER BY id ASC");
SQLServerBulkCopy bulkCopy3 = new SQLServerBulkCopy(conn)) {
bulkCopy3.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy3.writeToServer(rs2);
Expand All @@ -170,20 +170,20 @@ public void testMultiplePreparedStatementAndResultSet() throws SQLException {

private static void verifyDestinationTableData(int expectedNumberOfRows) throws SQLException {
try (Connection conn = DriverManager.getConnection(connectionString); ResultSet rs = conn.createStatement()
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(desTable))) {
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(desTable) + " ORDER BY id ASC")) {

int expectedArrayLength = expectedBigDecimals.length;

int i = 0;
while (rs.next()) {
assertTrue(rs.getString(1).equals(expectedBigDecimalStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedBigDecimalStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(1));
assertTrue(rs.getString(2).trim().equals(expectedStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(2));
assertTrue(rs.getString(3).equals(expectedTimestampStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedTimestampStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(3));
assertTrue(rs.getString(4).trim().equals(expectedStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(4));
assertTrue(rs.getString(2).equals(expectedBigDecimalStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedBigDecimalStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(2));
assertTrue(rs.getString(3).trim().equals(expectedStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(3));
assertTrue(rs.getString(4).equals(expectedTimestampStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedTimestampStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(4));
assertTrue(rs.getString(5).trim().equals(expectedStrings[i % expectedArrayLength]), "Expected Value:"
+ expectedStrings[i % expectedArrayLength] + ", Actual Value: " + rs.getString(5));
i++;
}

Expand All @@ -196,8 +196,7 @@ private static void populateSourceTable() throws SQLException {
Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

try (Connection conn = DriverManager.getConnection(connectionString);
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement(sql)) {

SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement(sql)) {
for (int i = 0; i < expectedBigDecimals.length; i++) {
pstmt.setBigDecimal(1, expectedBigDecimals[i]);
pstmt.setString(2, expectedStrings[i]);
Expand All @@ -219,10 +218,10 @@ private static void dropTables(Statement stmt) throws SQLException {

private static void createTables(Statement stmt) throws SQLException {
String sql = "create table " + AbstractSQLGenerator.escapeIdentifier(srcTable)
+ " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
+ " (id INT NOT NULL IDENTITY PRIMARY KEY, c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
stmt.execute(sql);
sql = "create table " + AbstractSQLGenerator.escapeIdentifier(desTable)
+ " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
+ " (id INT NOT NULL IDENTITY PRIMARY KEY, c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
stmt.execute(sql);
}

Expand Down

0 comments on commit 12e4e49

Please sign in to comment.