Skip to content

Commit dc5dd51

Browse files
committed
Update tests on scan queries inside transactions
1 parent c89847b commit dc5dd51

File tree

7 files changed

+207
-105
lines changed

7 files changed

+207
-105
lines changed

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbJdbcTools.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package tech.ydb.jdbc.settings;
22

3+
import java.io.UnsupportedEncodingException;
34
import java.net.URI;
45
import java.net.URISyntaxException;
56
import java.net.URLEncoder;
6-
import java.nio.charset.StandardCharsets;
7-
import java.io.UnsupportedEncodingException;
87
import java.sql.SQLException;
98
import java.util.Collection;
109
import java.util.Collections;

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbOperationProperties.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public YdbOperationProperties(Map<YdbOperationProperty<?>, ParsedProperty> param
4545

4646
this.scanQueryTxMode = params.get(YdbOperationProperty.SCAN_QUERY_TX_MODE).getParsedValue();
4747
this.schemeQueryTxMode = params.get(YdbOperationProperty.SCHEME_QUERY_TX_MODE).getParsedValue();
48-
this.forcedQueryType = params.get(YdbOperationProperty.FORCE_QUERY_MODE).getParsedValue();
48+
49+
ParsedProperty forcedType = params.get(YdbOperationProperty.FORCE_QUERY_MODE);
50+
this.forcedQueryType = forcedType != null ? forcedType.getParsedValue() : null;
4951
}
5052

5153
public Map<YdbOperationProperty<?>, ParsedProperty> getParams() {

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java

Lines changed: 118 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void dropTable() throws SQLException {
6666

6767
@BeforeEach
6868
public void checkTransactionState() throws SQLException {
69-
Assertions.assertNull(currentTxId(), "Transaction must be empty before test");
69+
Assertions.assertNull(getTxId(jdbc.connection()), "Transaction must be empty before test");
7070
}
7171

7272
@AfterEach
@@ -89,8 +89,8 @@ private void cleanTable() throws SQLException {
8989
}
9090
}
9191

92-
private String currentTxId() throws SQLException {
93-
return jdbc.connection().unwrap(YdbConnection.class).getYdbTxId();
92+
private String getTxId(Connection connection) throws SQLException {
93+
return connection.unwrap(YdbConnection.class).getYdbTxId();
9494
}
9595

9696
@Test
@@ -246,14 +246,14 @@ public void invalidSQLCancelTransaction() throws SQLException {
246246
jdbc.connection().setAutoCommit(false);
247247
try (Statement statement = jdbc.connection().createStatement()) {
248248
statement.execute(SELECT_2_2);
249-
String txId = currentTxId();
249+
String txId = getTxId(jdbc.connection());
250250
Assertions.assertNotNull(txId);
251251

252252
ExceptionAssert.ydbNonRetryable("Column reference 'x' (S_ERROR)", () -> statement.execute("select 2 + x"));
253253

254254
statement.execute(SELECT_2_2);
255-
Assertions.assertNotNull(currentTxId());
256-
Assertions.assertNotEquals(txId, currentTxId());
255+
Assertions.assertNotNull(getTxId(jdbc.connection()));
256+
Assertions.assertNotEquals(txId, getTxId(jdbc.connection()));
257257
} finally {
258258
jdbc.connection().setAutoCommit(true);
259259
}
@@ -264,28 +264,28 @@ public void autoCommit() throws SQLException {
264264
jdbc.connection().setAutoCommit(false);
265265
try (Statement statement = jdbc.connection().createStatement()) {
266266
statement.execute(SELECT_2_2);
267-
String txId = currentTxId();
267+
String txId = getTxId(jdbc.connection());
268268
Assertions.assertNotNull(txId);
269269

270270
Assertions.assertFalse(jdbc.connection().getAutoCommit());
271271

272272
jdbc.connection().setAutoCommit(false);
273273
Assertions.assertFalse(jdbc.connection().getAutoCommit());
274-
Assertions.assertEquals(txId, currentTxId());
274+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
275275

276276
jdbc.connection().setAutoCommit(true);
277277
Assertions.assertTrue(jdbc.connection().getAutoCommit());
278-
Assertions.assertNull(currentTxId());
278+
Assertions.assertNull(getTxId(jdbc.connection()));
279279

280280
statement.execute(SELECT_2_2);
281-
Assertions.assertNull(currentTxId());
281+
Assertions.assertNull(getTxId(jdbc.connection()));
282282

283283
statement.execute(SELECT_2_2);
284-
Assertions.assertNull(currentTxId());
284+
Assertions.assertNull(getTxId(jdbc.connection()));
285285

286286
jdbc.connection().setAutoCommit(false);
287287
Assertions.assertFalse(jdbc.connection().getAutoCommit());
288-
Assertions.assertNull(currentTxId());
288+
Assertions.assertNull(getTxId(jdbc.connection()));
289289
} finally {
290290
jdbc.connection().setAutoCommit(true);
291291
}
@@ -296,26 +296,26 @@ public void commit() throws SQLException {
296296
jdbc.connection().setAutoCommit(false);
297297
try (Statement statement = jdbc.connection().createStatement()) {
298298
Assertions.assertTrue(statement.execute(SELECT_2_2));
299-
String txId = currentTxId();
299+
String txId = getTxId(jdbc.connection());
300300
Assertions.assertNotNull(txId);
301301

302302
Assertions.assertTrue(statement.execute(SELECT_2_2));
303-
Assertions.assertEquals(txId, currentTxId());
303+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
304304

305305
Assertions.assertTrue(statement.execute(QUERIES.selectAllSQL()));
306-
Assertions.assertEquals(txId, currentTxId());
306+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
307307

308308
Assertions.assertFalse(statement.execute(SIMPLE_UPSERT));
309-
Assertions.assertEquals(txId, currentTxId());
309+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
310310

311311
Assertions.assertTrue(statement.execute(SELECT_2_2));
312-
Assertions.assertEquals(txId, currentTxId());
312+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
313313

314314
jdbc.connection().commit();
315-
Assertions.assertNull(currentTxId());
315+
Assertions.assertNull(getTxId(jdbc.connection()));
316316

317317
jdbc.connection().commit(); // does nothing
318-
Assertions.assertNull(currentTxId());
318+
Assertions.assertNull(getTxId(jdbc.connection()));
319319

320320
try (ResultSet result = statement.executeQuery(QUERIES.selectAllSQL())) {
321321
Assertions.assertTrue(result.next());
@@ -327,31 +327,32 @@ public void commit() throws SQLException {
327327
}
328328

329329
@Test
330-
public void ddlAutoCommit() throws SQLException {
330+
public void schemeQueryInTx() throws SQLException {
331331
jdbc.connection().setAutoCommit(false);
332332
try (Statement statement = jdbc.connection().createStatement()) {
333333
Assertions.assertTrue(statement.execute(SELECT_2_2));
334-
String txId = currentTxId();
334+
String txId = getTxId(jdbc.connection());
335335
Assertions.assertNotNull(txId);
336336

337337
Assertions.assertTrue(statement.execute(SELECT_2_2));
338-
Assertions.assertEquals(txId, currentTxId());
338+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
339339

340340
Assertions.assertTrue(statement.execute(QUERIES.selectAllSQL()));
341-
Assertions.assertEquals(txId, currentTxId());
341+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
342342

343343
Assertions.assertFalse(statement.execute(SIMPLE_UPSERT));
344-
Assertions.assertEquals(txId, currentTxId());
344+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
345345

346346
Assertions.assertTrue(statement.execute(SELECT_2_2));
347-
Assertions.assertEquals(txId, currentTxId());
347+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
348348

349-
// DDL - equals to commit
350-
statement.execute("CREATE TABLE tmp_table (id Int32, primary key (id))");
351-
Assertions.assertNull(currentTxId());
349+
ExceptionAssert.sqlException(YdbConst.SCHEME_QUERY_INSIDE_TRANSACTION,
350+
() -> statement.execute("CREATE TABLE tmp_table (id Int32, primary key (id))"));
351+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
352352

353-
statement.execute("DROP TABLE tmp_table");
354-
Assertions.assertNull(currentTxId());
353+
ExceptionAssert.sqlException(YdbConst.SCHEME_QUERY_INSIDE_TRANSACTION,
354+
() -> statement.execute("DROP TABLE tmp_table"));
355+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
355356

356357
try (ResultSet result = statement.executeQuery(QUERIES.selectAllSQL())) {
357358
Assertions.assertTrue(result.next());
@@ -362,31 +363,107 @@ public void ddlAutoCommit() throws SQLException {
362363
}
363364
}
364365

366+
@Test
367+
public void schemeQueryWithShadowCommit() throws SQLException {
368+
try (Connection connection = jdbc.createCustomConnection("schemeQueryTxMode", "SHADOW_COMMIT")) {
369+
connection.setAutoCommit(false);
370+
371+
try (Statement statement = connection.createStatement()) {
372+
Assertions.assertTrue(statement.execute(SELECT_2_2));
373+
String txId = getTxId(connection);
374+
Assertions.assertNotNull(txId);
375+
376+
Assertions.assertTrue(statement.execute(SELECT_2_2));
377+
Assertions.assertEquals(txId, getTxId(connection));
378+
379+
Assertions.assertTrue(statement.execute(QUERIES.selectAllSQL()));
380+
Assertions.assertEquals(txId, getTxId(connection));
381+
382+
Assertions.assertFalse(statement.execute(SIMPLE_UPSERT));
383+
Assertions.assertEquals(txId, getTxId(connection));
384+
385+
Assertions.assertTrue(statement.execute(SELECT_2_2));
386+
Assertions.assertEquals(txId, getTxId(connection));
387+
388+
// DDL - equals to commit
389+
statement.execute("CREATE TABLE tmp_table (id Int32, primary key (id))");
390+
Assertions.assertNull(getTxId(connection));
391+
392+
statement.execute("DROP TABLE tmp_table");
393+
Assertions.assertNull(getTxId(connection));
394+
395+
try (ResultSet result = statement.executeQuery(QUERIES.selectAllSQL())) {
396+
Assertions.assertTrue(result.next());
397+
}
398+
}
399+
} finally {
400+
cleanTable();
401+
}
402+
}
403+
404+
@Test
405+
public void schemeQueryInFakeTx() throws SQLException {
406+
try (Connection connection = jdbc.createCustomConnection("schemeQueryTxMode", "FAKE_TX")) {
407+
connection.setAutoCommit(false);
408+
409+
try (Statement statement = connection.createStatement()) {
410+
Assertions.assertTrue(statement.execute(SELECT_2_2));
411+
String txId = getTxId(connection);
412+
Assertions.assertNotNull(txId);
413+
414+
Assertions.assertTrue(statement.execute(SELECT_2_2));
415+
Assertions.assertEquals(txId, getTxId(connection));
416+
417+
Assertions.assertTrue(statement.execute(QUERIES.selectAllSQL()));
418+
Assertions.assertEquals(txId, getTxId(connection));
419+
420+
Assertions.assertFalse(statement.execute(SIMPLE_UPSERT));
421+
Assertions.assertEquals(txId, getTxId(connection));
422+
423+
Assertions.assertTrue(statement.execute(SELECT_2_2));
424+
Assertions.assertEquals(txId, getTxId(connection));
425+
426+
// FAKE TX - DDL will be executed outside current transaction
427+
statement.execute("CREATE TABLE tmp_table (id Int32, primary key (id))");
428+
Assertions.assertEquals(txId, getTxId(connection));
429+
430+
statement.execute("DROP TABLE tmp_table");
431+
Assertions.assertEquals(txId, getTxId(connection));
432+
433+
try (ResultSet result = statement.executeQuery(QUERIES.selectAllSQL())) {
434+
Assertions.assertTrue(result.next());
435+
}
436+
}
437+
} finally {
438+
cleanTable();
439+
}
440+
}
441+
365442
@Test
366443
public void rollback() throws SQLException {
367444
jdbc.connection().setAutoCommit(false);
368445
try (Statement statement = jdbc.connection().createStatement()) {
369446
Assertions.assertTrue(statement.execute(SELECT_2_2));
370-
String txId = currentTxId();
447+
String txId = getTxId(jdbc.connection());
371448
Assertions.assertNotNull(txId);
372449

373450
Assertions.assertTrue(statement.execute(SELECT_2_2));
374-
Assertions.assertEquals(txId, currentTxId());
451+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
375452

376453
Assertions.assertTrue(statement.execute(QUERIES.selectAllSQL()));
377-
Assertions.assertEquals(txId, currentTxId());
454+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
378455

379456
Assertions.assertFalse(statement.execute(SIMPLE_UPSERT));
380-
Assertions.assertEquals(txId, currentTxId());
457+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
381458

382459
Assertions.assertTrue(statement.execute(SELECT_2_2));
383-
Assertions.assertEquals(txId, currentTxId());
460+
Assertions.assertEquals(txId, getTxId(jdbc.connection()));
384461

385462
jdbc.connection().rollback();
386-
Assertions.assertNull(currentTxId());
463+
Assertions.assertNull(getTxId(jdbc.connection()));
387464

388465
jdbc.connection().rollback(); // does nothing
389-
Assertions.assertNull(currentTxId());
466+
Assertions.assertNull(getTxId(jdbc.connection()));
390467

391468
try (ResultSet result = statement.executeQuery(QUERIES.selectAllSQL())) {
392469
Assertions.assertFalse(result.next());
@@ -406,10 +483,10 @@ public void commitInvalidTx() throws SQLException {
406483
ExceptionAssert.ydbNonRetryable("Member not found: key2. Did you mean key?",
407484
() -> statement.executeQuery(QUERIES.wrongSelectSQL()));
408485

409-
Assertions.assertNull(currentTxId());
486+
Assertions.assertNull(getTxId(jdbc.connection()));
410487

411488
jdbc.connection().commit(); // Nothing to commit, transaction was rolled back already
412-
Assertions.assertNull(currentTxId());
489+
Assertions.assertNull(getTxId(jdbc.connection()));
413490
} finally {
414491
cleanTable();
415492
jdbc.connection().setAutoCommit(true);
@@ -428,9 +505,9 @@ public void rollbackInvalidTx() throws SQLException {
428505
ExceptionAssert.ydbNonRetryable("Member not found: key2. Did you mean key?",
429506
() -> statement.executeQuery(QUERIES.wrongSelectSQL()));
430507

431-
Assertions.assertNull(currentTxId());
508+
Assertions.assertNull(getTxId(jdbc.connection()));
432509
jdbc.connection().rollback();
433-
Assertions.assertNull(currentTxId());
510+
Assertions.assertNull(getTxId(jdbc.connection()));
434511
} finally {
435512
cleanTable();
436513
jdbc.connection().setAutoCommit(true);

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementImplTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,11 @@ public void executeQueryInTx(SqlQueries.YqlQuery mode) throws SQLException {
388388
@ParameterizedTest(name = "with {0}")
389389
@EnumSource(SqlQueries.YqlQuery.class)
390390
public void executeScanQueryInTx(SqlQueries.YqlQuery mode) throws SQLException {
391-
jdbc.connection().setAutoCommit(false);
392-
YdbConnection conn = jdbc.connection().unwrap(YdbConnection.class);
393391
String upsertYql = TEST_TABLE.upsertOne(mode, "c_Text", "Text");
394392
String scanSelectYql = scanSelectByKey("c_Text");
393+
394+
jdbc.connection().setAutoCommit(false);
395+
YdbConnection conn = jdbc.connection().unwrap(YdbConnection.class);
395396
try {
396397
try (YdbPreparedStatement statement = conn.prepareStatement(upsertYql)) {
397398
statement.setInt("key", 1);
@@ -401,8 +402,8 @@ public void executeScanQueryInTx(SqlQueries.YqlQuery mode) throws SQLException {
401402

402403
try (YdbPreparedStatement select = conn.prepareStatement(scanSelectYql)) {
403404
select.setInt("key", 1);
404-
TextSelectAssert.of(select.executeQuery(), "c_Text", "Text")
405-
.noNextRows();
405+
406+
ExceptionAssert.sqlException(YdbConst.SCAN_QUERY_INSIDE_TRANSACTION, () -> select.executeQuery());
406407

407408
jdbc.connection().commit();
408409

0 commit comments

Comments
 (0)