Skip to content

Commit

Permalink
Merge pull request ydb-platform#44 from ydb-platform/release_v2.0.6
Browse files Browse the repository at this point in the history
Release v2.0.6
  • Loading branch information
alex268 authored Jan 30, 2024
2 parents b492829 + 13b5159 commit bf9af1c
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 135 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.6 ##

* Upgraded Java SDK version
* Fixed Int6/Int16 auto detecting
* Fixed standard transaction levels

## 2.0.5 ##

* Extended usage of standard JDBC exception classes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ Specify the YDB JDBC driver in the dependencies:
<dependency>
<groupId>tech.ydb.jdbc</groupId>
<artifactId>ydb-jdbc-driver</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
</dependency>

<!-- Shaded version with included dependencies -->
<dependency>
<groupId>tech.ydb.jdbc</groupId>
<artifactId>ydb-jdbc-driver-shaded</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
</dependency>
</dependencies>
```
Expand Down
2 changes: 1 addition & 1 deletion jdbc-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>tech.ydb.jdbc</groupId>
<artifactId>ydb-jdbc-driver-parent</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
</parent>

<artifactId>ydb-jdbc-driver-shaded</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>tech.ydb.jdbc</groupId>
<artifactId>ydb-jdbc-driver-parent</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
</parent>

<artifactId>ydb-jdbc-driver</artifactId>
Expand Down
22 changes: 6 additions & 16 deletions jdbc/src/main/java/tech/ydb/jdbc/YdbConst.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package tech.ydb.jdbc;

import java.sql.Connection;

public final class YdbConst {

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

// Transaction levels
// See details in https://cloud.yandex.ru/docs/ydb/concepts/transactions

// Please note:
// Serializable transaction (RW) always uses "for-share" row-level locks before filter stage!
// Custom transaction levels
// See details in https://ydb.tech/docs/en/concepts/transactions#modes

/**
* All transactions are serialized one-by-one. If the DB detects a write collision among
* several concurrent transactions, only the first one is committed.
* This is the strongest level. And the only level at which data changes are possible.
*/
public static final int TRANSACTION_SERIALIZABLE_READ_WRITE = Connection.TRANSACTION_SERIALIZABLE;
/**
* The most recent consistent state of the database. Read only.
*/
public static final int ONLINE_CONSISTENT_READ_ONLY = Connection.TRANSACTION_REPEATABLE_READ;
public static final int ONLINE_CONSISTENT_READ_ONLY = 16;
/**
* The most recent inconsistent state of the database. Read only.
* A phantom read may occurs when, in the course of a transaction, some new rows are added
* by another transaction to the records being read. This is the weakest level.
*/
public static final int ONLINE_INCONSISTENT_READ_ONLY = Connection.TRANSACTION_READ_COMMITTED;
public static final int ONLINE_INCONSISTENT_READ_ONLY = ONLINE_CONSISTENT_READ_ONLY + 1;

/**
* An <em>almost</em> recent consistent state of the database. Read only.
* This level is faster then {@code ONLINE_CONSISTENT_READ_ONLY}, but may return stale data.
*/
public static final int STALE_CONSISTENT_READ_ONLY = 3; // TODO: verify if we can do that
public static final int STALE_CONSISTENT_READ_ONLY = 32;

// Processing queries
public static final String EXPLAIN_COLUMN_AST = "AST";
Expand Down
164 changes: 79 additions & 85 deletions jdbc/src/main/java/tech/ydb/jdbc/context/YdbTxState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.jdbc.context;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;

Expand All @@ -12,68 +13,90 @@
* @author Aleksandr Gorshenin
*/
public class YdbTxState {
private final int transactionLevel;
private final boolean isReadOnly;
private final boolean isAutoCommit;

private final TxControl<?> txControl;
protected final int transactionLevel;

protected YdbTxState(TxControl<?> tx, int level) {
this.txControl = tx;
protected YdbTxState(TxControl<?> txControl, int level, boolean isReadOnly, boolean isAutoCommit) {
this.transactionLevel = level;
this.isReadOnly = isReadOnly;
this.isAutoCommit = isAutoCommit;
this.txControl = txControl;
}

protected YdbTxState(TxControl<?> txControl, YdbTxState other) {
this.transactionLevel = other.transactionLevel;
this.isReadOnly = other.isReadOnly;
this.isAutoCommit = other.isAutoCommit;
this.txControl = txControl;
}

@Override
public String toString() {
return "NoTx" + transactionLevel;
return "NoTx";
}

public String txID() {
return null;
}

public boolean isInsideTransaction() {
return false;
}

public TxControl<?> txControl() {
return txControl;
}

public boolean isAutoCommit() {
return txControl.isCommitTx();
return isAutoCommit;
}

public boolean isReadOnly() {
return transactionLevel != YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE;
return isReadOnly;
}

public boolean isInsideTransaction() {
return false;
}

public int transactionLevel() throws SQLException {
public int transactionLevel() {
return transactionLevel;
}

public YdbTxState withAutoCommit(boolean newAutoCommit) throws SQLException {
if (newAutoCommit == isAutoCommit()) {
if (newAutoCommit == isAutoCommit) {
return this;
}
return create(transactionLevel(), newAutoCommit);

if (isInsideTransaction()) {
throw new SQLFeatureNotSupportedException(YdbConst.CHANGE_ISOLATION_INSIDE_TX);
}

return emptyTx(transactionLevel, isReadOnly, newAutoCommit);
}

public YdbTxState withReadOnly(boolean readOnly) throws SQLException {
if (readOnly == isReadOnly()) {
public YdbTxState withReadOnly(boolean newReadOnly) throws SQLException {
if (newReadOnly == isReadOnly()) {
return this;
}

if (readOnly) {
return create(YdbConst.ONLINE_CONSISTENT_READ_ONLY, isAutoCommit());
} else {
return create(YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE, isAutoCommit());
if (isInsideTransaction()) {
throw new SQLFeatureNotSupportedException(YdbConst.READONLY_INSIDE_TRANSACTION);
}

return emptyTx(transactionLevel, newReadOnly, isAutoCommit);
}

public YdbTxState withTransactionLevel(int newTransactionLevel) throws SQLException {
if (newTransactionLevel == transactionLevel()) {
if (newTransactionLevel == transactionLevel) {
return this;
}

return create(newTransactionLevel, isAutoCommit());
if (isInsideTransaction()) {
throw new SQLFeatureNotSupportedException(YdbConst.CHANGE_ISOLATION_INSIDE_TX);
}

boolean newReadOnly = isReadOnly || newTransactionLevel != Connection.TRANSACTION_SERIALIZABLE;
return emptyTx(newTransactionLevel, newReadOnly, isAutoCommit);
}

public YdbTxState withCommit(Session session) {
Expand All @@ -93,7 +116,7 @@ public YdbTxState withKeepAlive(Session session) {

public YdbTxState withDataQuery(Session session, String txID) {
if (txID != null && !txID.isEmpty()) {
return new TransactionInProgress(txID, session, isAutoCommit());
return new TransactionInProgress(txID, session, this);
}

session.close();
Expand All @@ -104,88 +127,59 @@ public Session getSession(YdbContext ctx, YdbExecutor executor) throws SQLExcept
return executor.createSession(ctx);
}

public static YdbTxState create(int level, boolean autoCommit) throws SQLException {
return create(null, null, level, autoCommit);
}
private static TxControl<?> txControl(int level, boolean isReadOnly, boolean isAutoCommit) throws SQLException {
if (!isReadOnly) {
// YDB support only one RW mode
if (level != Connection.TRANSACTION_SERIALIZABLE) {
throw new SQLException(YdbConst.UNSUPPORTED_TRANSACTION_LEVEL + level);
}

return TxControl.serializableRw().setCommitTx(isAutoCommit);
}

private static YdbTxState create(Session session, String txId, int level, boolean autoCommit)
throws SQLException {
switch (level) {
case YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE:
if (txId != null) {
return new TransactionInProgress(txId, session, autoCommit);
} else {
if (autoCommit) {
return new YdbTxState(TxControl.serializableRw(), level);
} else {
return new EmptyTransaction();
}
}
case Connection.TRANSACTION_SERIALIZABLE:
return TxControl.snapshotRo().setCommitTx(isAutoCommit);
case YdbConst.ONLINE_CONSISTENT_READ_ONLY:
return new YdbTxState(TxControl.onlineRo(), level);
case YdbConst.STALE_CONSISTENT_READ_ONLY:
return new YdbTxState(TxControl.staleRo(), level);
return TxControl.onlineRo().setAllowInconsistentReads(false).setCommitTx(isAutoCommit);
case YdbConst.ONLINE_INCONSISTENT_READ_ONLY:
return new YdbTxState(TxControl.onlineRo().setAllowInconsistentReads(true), level);
return TxControl.onlineRo().setAllowInconsistentReads(true).setCommitTx(isAutoCommit);
case YdbConst.STALE_CONSISTENT_READ_ONLY:
return TxControl.staleRo().setCommitTx(isAutoCommit);
default:
throw new SQLException(YdbConst.UNSUPPORTED_TRANSACTION_LEVEL + level);
}
}

private static class EmptyTransaction extends YdbTxState {
EmptyTransaction() {
super(TxControl.serializableRw().setCommitTx(false), YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE);
}

@Override
public String toString() {
return "EmptyTx" + transactionLevel;
}

@Override
public YdbTxState withDataQuery(Session session, String txID) {
if (txID != null && !txID.isEmpty()) {
return new TransactionInProgress(txID, session, isAutoCommit());
}
private static YdbTxState emptyTx(int level, boolean isReadOnly, boolean isAutoCommit) throws SQLException {
TxControl<?> tx = txControl(level, isReadOnly, isAutoCommit);
return new YdbTxState(tx, level, isReadOnly, isAutoCommit);
}

session.close();
return this;
}
public static YdbTxState create(int level, boolean isAutoCommit) throws SQLException {
return emptyTx(level, level != Connection.TRANSACTION_SERIALIZABLE, isAutoCommit);
}

private static class TransactionInProgress extends YdbTxState {
private final String id;
private final String txID;
private final Session session;
private final YdbTxState previos;

TransactionInProgress(String id, Session session, boolean autoCommit) {
super(TxControl.id(id).setCommitTx(autoCommit), YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE);
this.id = id;
TransactionInProgress(String id, Session session, YdbTxState previosState) {
super(TxControl.id(id).setCommitTx(previosState.isAutoCommit), previosState);
this.txID = id;
this.session = session;
this.previos = previosState;
}

@Override
public String toString() {
return "InTx" + transactionLevel + "[" + id + "]";
return "InTx" + transactionLevel() + "[" + txID + "]";
}

@Override
public String txID() {
return id;
}

@Override
public YdbTxState withAutoCommit(boolean newAutoCommit) throws SQLException {
throw new SQLFeatureNotSupportedException(YdbConst.CHANGE_ISOLATION_INSIDE_TX);
}

@Override
public YdbTxState withTransactionLevel(int newTransactionLevel) throws SQLException {
throw new SQLFeatureNotSupportedException(YdbConst.CHANGE_ISOLATION_INSIDE_TX);
}

@Override
public YdbTxState withReadOnly(boolean readOnly) throws SQLException {
throw new SQLFeatureNotSupportedException(YdbConst.READONLY_INSIDE_TRANSACTION);
return txID;
}

@Override
Expand All @@ -201,13 +195,13 @@ public Session getSession(YdbContext ctx, YdbExecutor executor) throws SQLExcept
@Override
public YdbTxState withCommit(Session session) {
session.close();
return new EmptyTransaction();
return previos;
}

@Override
public YdbTxState withRollback(Session session) {
session.close();
return new EmptyTransaction();
return previos;
}

@Override
Expand All @@ -222,15 +216,15 @@ public YdbTxState withDataQuery(Session session, String txID) {
session.close();
}
this.session.close();
return new EmptyTransaction();
return previos;
}

if (this.id.equals(txID)) {
if (txID.equals(txID())) {
if (this.session == session) {
return this;
}
this.session.close();
return new TransactionInProgress(txID, session, isAutoCommit());
return new TransactionInProgress(txID, session, previos);
}

session.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.jdbc.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
Expand Down Expand Up @@ -616,7 +617,7 @@ public int getMaxUserNameLength() {

@Override
public int getDefaultTransactionIsolation() {
return YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE;
return Connection.TRANSACTION_SERIALIZABLE;
}

@Override
Expand All @@ -627,7 +628,7 @@ public boolean supportsTransactions() {
@Override
public boolean supportsTransactionIsolationLevel(int level) {
switch (level) {
case YdbConst.TRANSACTION_SERIALIZABLE_READ_WRITE:
case Connection.TRANSACTION_SERIALIZABLE:
case YdbConst.ONLINE_CONSISTENT_READ_ONLY:
case YdbConst.ONLINE_INCONSISTENT_READ_ONLY:
case YdbConst.STALE_CONSISTENT_READ_ONLY:
Expand Down
Loading

0 comments on commit bf9af1c

Please sign in to comment.