Skip to content

Commit bdcf39a

Browse files
committed
Use custom codes for non standard transaction levels
1 parent 786cabe commit bdcf39a

File tree

6 files changed

+25
-34
lines changed

6 files changed

+25
-34
lines changed

jdbc/src/main/java/tech/ydb/jdbc/YdbConst.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package tech.ydb.jdbc;
22

3-
import java.sql.Connection;
4-
53
public final class YdbConst {
64

75
// SQL types
@@ -107,33 +105,25 @@ public final class YdbConst {
107105
public static final String MISSING_REQUIRED_VALUE = "Missing required value for parameter: ";
108106
public static final String INVALID_PARAMETER_TYPE = "Cannot cast parameter [%s] from [%s] to [%s]";
109107

110-
// Transaction levels
111-
// See details in https://cloud.yandex.ru/docs/ydb/concepts/transactions
112-
113-
// Please note:
114-
// Serializable transaction (RW) always uses "for-share" row-level locks before filter stage!
108+
// Custom transaction levels
109+
// See details in https://ydb.tech/docs/en/concepts/transactions#modes
115110

116-
/**
117-
* All transactions are serialized one-by-one. If the DB detects a write collision among
118-
* several concurrent transactions, only the first one is committed.
119-
* This is the strongest level. And the only level at which data changes are possible.
120-
*/
121-
public static final int TRANSACTION_SERIALIZABLE_READ_WRITE = Connection.TRANSACTION_SERIALIZABLE;
122111
/**
123112
* The most recent consistent state of the database. Read only.
124113
*/
125-
public static final int ONLINE_CONSISTENT_READ_ONLY = Connection.TRANSACTION_REPEATABLE_READ;
114+
public static final int ONLINE_CONSISTENT_READ_ONLY = 16;
126115
/**
127116
* The most recent inconsistent state of the database. Read only.
128117
* A phantom read may occurs when, in the course of a transaction, some new rows are added
129118
* by another transaction to the records being read. This is the weakest level.
130119
*/
131-
public static final int ONLINE_INCONSISTENT_READ_ONLY = Connection.TRANSACTION_READ_COMMITTED;
120+
public static final int ONLINE_INCONSISTENT_READ_ONLY = ONLINE_CONSISTENT_READ_ONLY + 1;
121+
132122
/**
133123
* An <em>almost</em> recent consistent state of the database. Read only.
134124
* This level is faster then {@code ONLINE_CONSISTENT_READ_ONLY}, but may return stale data.
135125
*/
136-
public static final int STALE_CONSISTENT_READ_ONLY = 3; // TODO: verify if we can do that
126+
public static final int STALE_CONSISTENT_READ_ONLY = 32;
137127

138128
// Processing queries
139129
public static final String EXPLAIN_COLUMN_AST = "AST";

jdbc/src/main/java/tech/ydb/jdbc/context/YdbTxState.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.ydb.jdbc.context;
22

3+
import java.sql.Connection;
34
import java.sql.SQLException;
45
import java.sql.SQLFeatureNotSupportedException;
56

@@ -38,7 +39,7 @@ public boolean isAutoCommit() {
3839
}
3940

4041
public boolean isReadOnly() {
41-
return transactionLevel != YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE;
42+
return transactionLevel != Connection.TRANSACTION_SERIALIZABLE;
4243
}
4344

4445
public boolean isInsideTransaction() {
@@ -64,7 +65,7 @@ public YdbTxState withReadOnly(boolean readOnly) throws SQLException {
6465
if (readOnly) {
6566
return create(YdbConst.ONLINE_CONSISTENT_READ_ONLY, isAutoCommit());
6667
} else {
67-
return create(YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE, isAutoCommit());
68+
return create(Connection.TRANSACTION_SERIALIZABLE, isAutoCommit());
6869
}
6970
}
7071

@@ -111,7 +112,7 @@ public static YdbTxState create(int level, boolean autoCommit) throws SQLExcepti
111112
private static YdbTxState create(Session session, String txId, int level, boolean autoCommit)
112113
throws SQLException {
113114
switch (level) {
114-
case YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE:
115+
case Connection.TRANSACTION_SERIALIZABLE:
115116
if (txId != null) {
116117
return new TransactionInProgress(txId, session, autoCommit);
117118
} else {
@@ -134,7 +135,7 @@ private static YdbTxState create(Session session, String txId, int level, boolea
134135

135136
private static class EmptyTransaction extends YdbTxState {
136137
EmptyTransaction() {
137-
super(TxControl.serializableRw().setCommitTx(false), YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE);
138+
super(TxControl.serializableRw().setCommitTx(false), Connection.TRANSACTION_SERIALIZABLE);
138139
}
139140

140141
@Override
@@ -158,7 +159,7 @@ private static class TransactionInProgress extends YdbTxState {
158159
private final Session session;
159160

160161
TransactionInProgress(String id, Session session, boolean autoCommit) {
161-
super(TxControl.id(id).setCommitTx(autoCommit), YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE);
162+
super(TxControl.id(id).setCommitTx(autoCommit), Connection.TRANSACTION_SERIALIZABLE);
162163
this.id = id;
163164
this.session = session;
164165
}

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbDatabaseMetaDataImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.ydb.jdbc.impl;
22

3+
import java.sql.Connection;
34
import java.sql.ResultSet;
45
import java.sql.RowIdLifetime;
56
import java.sql.SQLException;
@@ -616,7 +617,7 @@ public int getMaxUserNameLength() {
616617

617618
@Override
618619
public int getDefaultTransactionIsolation() {
619-
return YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE;
620+
return Connection.TRANSACTION_SERIALIZABLE;
620621
}
621622

622623
@Override
@@ -627,7 +628,7 @@ public boolean supportsTransactions() {
627628
@Override
628629
public boolean supportsTransactionIsolationLevel(int level) {
629630
switch (level) {
630-
case YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE:
631+
case Connection.TRANSACTION_SERIALIZABLE:
631632
case YdbConst.ONLINE_CONSISTENT_READ_ONLY:
632633
case YdbConst.ONLINE_INCONSISTENT_READ_ONLY:
633634
case YdbConst.STALE_CONSISTENT_READ_ONLY:

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverProperitesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static Properties customizedProperties() {
366366
properties.setProperty("sessionTimeout", "6s");
367367
properties.setProperty("deadlineTimeout", "1s");
368368
properties.setProperty("autoCommit", "true");
369-
properties.setProperty("transactionLevel", "4");
369+
properties.setProperty("transactionLevel", "16");
370370

371371
properties.setProperty("cacheConnectionsInDriver", "false");
372372
properties.setProperty("preparedStatementCacheQueries", "100");
@@ -407,7 +407,7 @@ static DriverPropertyInfo[] customizedPropertyInfo() {
407407
YdbOperationProperty.SESSION_TIMEOUT.toDriverPropertyInfo("6s"),
408408
YdbOperationProperty.DEADLINE_TIMEOUT.toDriverPropertyInfo("1s"),
409409
YdbOperationProperty.AUTOCOMMIT.toDriverPropertyInfo("true"),
410-
YdbOperationProperty.TRANSACTION_LEVEL.toDriverPropertyInfo("4"),
410+
YdbOperationProperty.TRANSACTION_LEVEL.toDriverPropertyInfo("16"),
411411

412412
YdbOperationProperty.CACHE_CONNECTIONS_IN_DRIVER.toDriverPropertyInfo("false"),
413413
YdbOperationProperty.PREPARED_STATEMENT_CACHE_SIZE.toDriverPropertyInfo("100"),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public void readOnly() throws SQLException {
536536
jdbc.connection().setReadOnly(true);
537537

538538
Assertions.assertTrue(jdbc.connection().isReadOnly());
539-
Assertions.assertEquals(Connection.TRANSACTION_REPEATABLE_READ, jdbc.connection().getTransactionIsolation());
539+
Assertions.assertEquals(Connection.TRANSACTION_SERIALIZABLE, jdbc.connection().getTransactionIsolation());
540540

541541
jdbc.connection().setReadOnly(false);
542542
Assertions.assertFalse(jdbc.connection().isReadOnly());
@@ -559,10 +559,10 @@ public void schema() throws SQLException {
559559

560560
@ParameterizedTest(name = "Check supported isolation level {0}")
561561
@ValueSource(ints = {
562-
YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE, // 8
563-
YdbConst.ONLINE_CONSISTENT_READ_ONLY, // 4
564-
YdbConst.ONLINE_INCONSISTENT_READ_ONLY, // 2
565-
YdbConst.STALE_CONSISTENT_READ_ONLY // 3
562+
Connection.TRANSACTION_SERIALIZABLE, // 8
563+
YdbConst.ONLINE_CONSISTENT_READ_ONLY, // 16
564+
YdbConst.ONLINE_INCONSISTENT_READ_ONLY, // 17
565+
YdbConst.STALE_CONSISTENT_READ_ONLY // 32
566566
})
567567
public void supportedTransactionIsolations(int level) throws SQLException {
568568
jdbc.connection().setTransactionIsolation(level);
@@ -574,7 +574,7 @@ public void supportedTransactionIsolations(int level) throws SQLException {
574574
}
575575

576576
@ParameterizedTest(name = "Check supported isolation level {0}")
577-
@ValueSource(ints = { 0, 1, /*2, 3, 4,*/ 5, 6, 7, /*8,*/ 9, 10 })
577+
@ValueSource(ints = { 0, 1, 2, 3, 4, 5, 6, 7, /*8,*/ 9, 10 })
578578
public void unsupportedTransactionIsolations(int level) throws SQLException {
579579
ExceptionAssert.sqlException("Unsupported transaction level: " + level,
580580
() -> jdbc.connection().setTransactionIsolation(level)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,10 @@ public void metaDataValuesTest() throws SQLException {
251251
Assertions.assertEquals(YdbConst.MAX_ELEMENT_NAME_LENGTH, metaData.getMaxTableNameLength());
252252
Assertions.assertEquals(0, metaData.getMaxStatements());
253253
Assertions.assertEquals(YdbConst.MAX_ELEMENT_NAME_LENGTH, metaData.getMaxTableNameLength());
254-
Assertions.assertEquals(
255-
YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE, metaData.getDefaultTransactionIsolation());
254+
Assertions.assertEquals(Connection.TRANSACTION_SERIALIZABLE, metaData.getDefaultTransactionIsolation());
256255

257256
Assertions.assertTrue(metaData.supportsTransactions());
258-
Assertions.assertTrue(metaData.supportsTransactionIsolationLevel(YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE));
257+
Assertions.assertTrue(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE));
259258
Assertions.assertTrue(metaData.supportsTransactionIsolationLevel(YdbConst.ONLINE_CONSISTENT_READ_ONLY));
260259
Assertions.assertTrue(metaData.supportsTransactionIsolationLevel(YdbConst.ONLINE_INCONSISTENT_READ_ONLY));
261260
Assertions.assertTrue(metaData.supportsTransactionIsolationLevel(YdbConst.STALE_CONSISTENT_READ_ONLY));

0 commit comments

Comments
 (0)