/
BatchExecutionTest.java
326 lines (272 loc) · 12.8 KB
/
BatchExecutionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc.unit.statement;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.opentest4j.TestAbortedException;
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;
import com.microsoft.sqlserver.testframework.PrepUtil;
/**
* Tests batch execution with AE On connection
*
*/
@RunWith(JUnitPlatform.class)
@Tag(Constants.xSQLv11)
@Tag(Constants.xSQLv12)
@Tag(Constants.xAzureSQLDW)
public class BatchExecutionTest extends AbstractTest {
static String ctstable1;
static String ctstable2;
/**
* testAddBatch1 and testExecutionBatch one looks similar except for the parameters being passed for select query.
* TODO: we should look and simply the test later by parameterized values
*
* @throws Exception
*/
@Test
public void testBatchExceptionAEOn() throws Exception {
testAddBatch1();
testExecuteBatch1();
testAddBatch1UseBulkCopyAPI();
testExecuteBatch1UseBulkCopyAPI();
}
@Test
public void testBatchStatementCancellation() throws Exception {
try (Connection connection = PrepUtil.getConnection(connectionString)) {
connection.setAutoCommit(false);
try (PreparedStatement statement = connection.prepareStatement(
"if object_id('test_table') is not null drop table test_table")) {
statement.execute();
}
connection.commit();
try (PreparedStatement statement = connection.prepareStatement(
"create table test_table (column_name bit)")) {
statement.execute();
}
connection.commit();
for (long delayInMilliseconds : new long[] { 1, 2, 4, 8, 16, 32, 64, 128 }) {
for (int numberOfCommands : new int[] { 1, 2, 4, 8, 16, 32, 64 }) {
int parameterCount = 512;
try (PreparedStatement statement = connection.prepareStatement(
"insert into test_table values (?)" + repeat(",(?)", parameterCount - 1))) {
for (int i = 0; i < numberOfCommands; i++) {
for (int j = 0; j < parameterCount; j++) {
statement.setBoolean(j + 1, true);
}
statement.addBatch();
}
Thread cancelThread = cancelAsync(statement, delayInMilliseconds);
try {
statement.executeBatch();
} catch (SQLException e) {
assertEquals(TestResource.getResource("R_queryCancelled"), e.getMessage());
}
cancelThread.join();
}
connection.commit();
}
}
}
}
/**
* Get a PreparedStatement object and call the addBatch() method with 3 SQL statements and call the executeBatch()
* method and it should return array of Integer values of length 3
*/
public void testAddBatch1() {
testAddBatch1Internal("BatchInsert");
}
public void testAddBatch1UseBulkCopyAPI() {
testAddBatch1Internal("BulkCopy");
}
/**
* Get a PreparedStatement object and call the addBatch() method with a 3 valid SQL statements and call the
* executeBatch() method It should return an array of Integer values of length 3.
*/
public void testExecuteBatch1() {
testExecuteBatch1Internal("BatchInsert");
}
public void testExecuteBatch1UseBulkCopyAPI() {
testExecuteBatch1Internal("BulkCopy");
}
private void testExecuteBatch1Internal(String mode) {
int i = 0;
int retValue[] = {0, 0, 0};
int updateCountlen = 0;
try (Connection connection = PrepUtil.getConnection(connectionString + ";columnEncryptionSetting=Enabled;");) {
String sPrepStmt = "update " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " set PRICE=PRICE*20 where TYPE_ID=?";
if (mode.equalsIgnoreCase("bulkcopy")) {
modifyConnectionForBulkCopyAPI((SQLServerConnection) connection);
}
try (PreparedStatement pstmt = connection.prepareStatement(sPrepStmt)) {
pstmt.setInt(1, 1);
pstmt.addBatch();
pstmt.setInt(1, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.addBatch();
int[] updateCount = pstmt.executeBatch();
updateCountlen = updateCount.length;
assertTrue(updateCountlen == 3, TestResource.getResource("R_executeBatchFailed") + ": "
+ TestResource.getResource("R_incorrectUpdateCount"));
String sPrepStmt1 = "select count(*) from " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " where TYPE_ID=?";
try (PreparedStatement pstmt1 = connection.prepareStatement(sPrepStmt1)) {
for (int n = 1; n <= 3; n++) {
pstmt1.setInt(1, n);
try (ResultSet rs = pstmt1.executeQuery()) {
rs.next();
retValue[i++] = rs.getInt(1);
}
}
}
for (int j = 0; j < updateCount.length; j++) {
if (updateCount[j] != retValue[j] && updateCount[j] != Statement.SUCCESS_NO_INFO) {
fail(TestResource.getResource("R_executeBatchFailed") + ": "
+ TestResource.getResource("R_incorrectUpdateCount"));
}
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_executeBatchFailed") + ": " + e.getMessage());
}
}
private static void createTable() throws SQLException {
try (Connection connection = PrepUtil.getConnection(connectionString + ";columnEncryptionSetting=Enabled;");
Statement stmt = (SQLServerStatement) connection.createStatement()) {
String sql1 = "create table " + AbstractSQLGenerator.escapeIdentifier(ctstable1)
+ " (TYPE_ID int, TYPE_DESC varchar(32), primary key(TYPE_ID)) ";
String sql2 = "create table " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " (KEY_ID int, COF_NAME varchar(32), PRICE float, TYPE_ID int, primary key(KEY_ID), foreign key(TYPE_ID) references "
+ AbstractSQLGenerator.escapeIdentifier(ctstable1) + ")";
stmt.execute(sql1);
stmt.execute(sql2);
String sqlin2 = "insert into " + AbstractSQLGenerator.escapeIdentifier(ctstable1)
+ " values (1,'COFFEE-Desc')";
stmt.execute(sqlin2);
sqlin2 = "insert into " + AbstractSQLGenerator.escapeIdentifier(ctstable1) + " values (2,'COFFEE-Desc2')";
stmt.execute(sqlin2);
sqlin2 = "insert into " + AbstractSQLGenerator.escapeIdentifier(ctstable1) + " values (3,'COFFEE-Desc3')";
stmt.execute(sqlin2);
String sqlin1 = "insert into " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " values (9,'COFFEE-9',9.0, 1)";
stmt.execute(sqlin1);
sqlin1 = "insert into " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " values (10,'COFFEE-10',10.0, 2)";
stmt.execute(sqlin1);
sqlin1 = "insert into " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " values (11,'COFFEE-11',11.0, 3)";
stmt.execute(sqlin1);
}
}
private void testAddBatch1Internal(String mode) {
int i = 0;
int retValue[] = {0, 0, 0};
try (Connection connection = PrepUtil.getConnection(connectionString + ";columnEncryptionSetting=Enabled;");) {
String sPrepStmt = "update " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " set PRICE=PRICE*20 where TYPE_ID=?";
if (mode.equalsIgnoreCase("bulkcopy")) {
modifyConnectionForBulkCopyAPI((SQLServerConnection) connection);
}
try (PreparedStatement pstmt = connection.prepareStatement(sPrepStmt)) {
pstmt.setInt(1, 2);
pstmt.addBatch();
pstmt.setInt(1, 3);
pstmt.addBatch();
pstmt.setInt(1, 4);
pstmt.addBatch();
int[] updateCount = pstmt.executeBatch();
int updateCountlen = updateCount.length;
assertTrue(updateCountlen == 3, TestResource.getResource("R_addBatchFailed") + ": "
+ TestResource.getResource("R_incorrectUpdateCount"));
String sPrepStmt1 = "select count(*) from " + AbstractSQLGenerator.escapeIdentifier(ctstable2)
+ " where TYPE_ID=?";
try (PreparedStatement pstmt1 = connection.prepareStatement(sPrepStmt1)) {
// 2 is the number that is set First for Type Id in Prepared Statement
for (int n = 2; n <= 4; n++) {
pstmt1.setInt(1, n);
try (ResultSet rs = pstmt1.executeQuery()) {
rs.next();
retValue[i++] = rs.getInt(1);
}
}
}
for (int j = 0; j < updateCount.length; j++) {
if (updateCount[j] != retValue[j] && updateCount[j] != Statement.SUCCESS_NO_INFO) {
fail(TestResource.getResource("R_incorrectUpdateCount"));
}
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_addBatchFailed") + ": " + e.getMessage());
}
}
private void modifyConnectionForBulkCopyAPI(SQLServerConnection con) throws Exception {
Field f1 = SQLServerConnection.class.getDeclaredField("isAzureDW");
f1.setAccessible(true);
f1.set(con, true);
con.setUseBulkCopyForBatchInsert(true);
}
private static String repeat(String string, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(string);
}
return sb.toString();
}
private static Thread cancelAsync(Statement statement, long delayInMilliseconds) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(delayInMilliseconds);
statement.cancel();
} catch (SQLException | InterruptedException e) {
// does not/must not happen
e.printStackTrace();
throw new IllegalStateException(e);
}
});
thread.start();
return thread;
}
@BeforeAll
public static void testSetup() throws TestAbortedException, Exception {
connectionString = TestUtils.addOrOverrideProperty(connectionString,"trustServerCertificate", "true");
setConnection();
ctstable1 = RandomUtil.getIdentifier("ctstable1");
ctstable2 = RandomUtil.getIdentifier("ctstable2");
dropTable();
createTable();
}
private static void dropTable() throws SQLException {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(ctstable2), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(ctstable1), stmt);
}
}
@AfterAll
public static void terminateVariation() throws Exception {
dropTable();
}
}