Skip to content

Commit 40c8d29

Browse files
committed
Added wait of statement closing
1 parent 47a4c8f commit 40c8d29

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
import tech.ydb.jdbc.context.YdbExecutor;
1010

1111
public interface YdbConnection extends Connection {
12-
/**
13-
* Return current YDB transaction, if exists
14-
*
15-
* @return YDB transaction ID or null, if no transaction started
16-
*/
1712
@Nullable
18-
String getYdbTxId();
13+
String getYdbTxId() throws SQLException;
1914

2015
YdbContext getCtx();
2116

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ public interface YdbStatement extends Statement {
4343

4444
@Override
4545
YdbConnection getConnection() throws SQLException;
46+
47+
void waitReady() throws SQLException;
4648
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,18 @@ private void ensureOpened() throws SQLException {
148148
}
149149
}
150150

151+
@Override
152+
public void waitReady() throws SQLException {
153+
state.close();
154+
}
155+
151156
protected void cleanState() throws SQLException {
152157
ensureOpened();
153-
clearWarnings();
158+
154159
state.close();
155160
state = YdbQueryResult.EMPTY;
161+
162+
clearWarnings();
156163
}
157164

158165
protected boolean updateState(YdbQueryResult result) throws SQLException {

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Map;
1818
import java.util.Properties;
1919
import java.util.concurrent.Executor;
20+
import java.util.concurrent.atomic.AtomicReference;
2021
import java.util.logging.Level;
2122
import java.util.logging.Logger;
2223

@@ -38,6 +39,7 @@ public class YdbConnectionImpl implements YdbConnection {
3839
private final YdbContext ctx;
3940
private final YdbValidator validator;
4041
private final YdbExecutor executor;
42+
private final AtomicReference<YdbStatement> currState = new AtomicReference<>();
4143

4244
public YdbConnectionImpl(YdbContext context) throws SQLException {
4345
this.ctx = context;
@@ -83,18 +85,35 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
8385
executor.setAutoCommit(autoCommit);
8486
}
8587

88+
private <T extends YdbStatement> T updateStatement(T statement) throws SQLException {
89+
Statement prev = currState.getAndSet(statement);
90+
if (prev != null) {
91+
prev.close();
92+
}
93+
return statement;
94+
}
95+
96+
private void waitStatementReady() throws SQLException {
97+
YdbStatement curr = currState.get();
98+
if (curr != null) {
99+
curr.waitReady();
100+
}
101+
}
102+
86103
@Override
87104
public boolean getAutoCommit() throws SQLException {
88105
return executor.isAutoCommit();
89106
}
90107

91108
@Override
92109
public void commit() throws SQLException {
110+
waitStatementReady();
93111
executor.commit(ctx, validator);
94112
}
95113

96114
@Override
97115
public void rollback() throws SQLException {
116+
waitStatementReady();
98117
executor.rollback(ctx, validator);
99118
}
100119

@@ -167,6 +186,8 @@ public SQLWarning getWarnings() throws SQLException {
167186
@Override
168187
public void clearWarnings() throws SQLException {
169188
executor.ensureOpened();
189+
190+
waitStatementReady();
170191
validator.clearWarnings();
171192
}
172193

@@ -210,7 +231,7 @@ public YdbStatement createStatement(int resultSetType, int resultSetConcurrency,
210231
int resultSetHoldability) throws SQLException {
211232
executor.ensureOpened();
212233
checkStatementParams(resultSetType, resultSetConcurrency, resultSetHoldability);
213-
return new YdbStatementImpl(this, resultSetType);
234+
return updateStatement(new YdbStatementImpl(this, resultSetType));
214235
}
215236

216237
@Override
@@ -242,7 +263,7 @@ private YdbPreparedStatement prepareStatement(String sql, int resultSetType, Ydb
242263
YdbQuery query = ctx.findOrParseYdbQuery(sql);
243264

244265
YdbPreparedQuery params = ctx.findOrPrepareParams(query, mode);
245-
return new YdbPreparedStatementImpl(this, query, params, resultSetType);
266+
return updateStatement(new YdbPreparedStatementImpl(this, query, params, resultSetType));
246267
}
247268

248269
@Override
@@ -287,7 +308,8 @@ public int getNetworkTimeout() throws SQLException {
287308
}
288309

289310
@Override
290-
public String getYdbTxId() {
311+
public String getYdbTxId() throws SQLException {
312+
waitStatementReady();
291313
return executor.txID();
292314
}
293315

0 commit comments

Comments
 (0)