Skip to content

Commit 55ff20b

Browse files
authored
Transaction States Should Be Checked In Queue (#189)
Motivation: Currently, `MySqlConnection` checks state between the `Mono.defer` is subscribed and `Exchangeable` is executed. It may cause undefined behavior. Modification: Checks transaction state when request queue executes task. Result: Resolves #183
1 parent 9ce085e commit 55ff20b

File tree

3 files changed

+127
-102
lines changed

3 files changed

+127
-102
lines changed

src/main/java/io/asyncer/r2dbc/mysql/ConnectionState.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ interface ConnectionState {
3030
*/
3131
void setIsolationLevel(IsolationLevel level);
3232

33+
/**
34+
* Reutrns session lock wait timeout.
35+
*
36+
* @return Session lock wait timeout.
37+
*/
38+
long getSessionLockWaitTimeout();
39+
3340
/**
3441
* Sets current lock wait timeout.
3542
*

src/main/java/io/asyncer/r2dbc/mysql/MySqlConnection.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public Mono<Void> close() {
209209
@Override
210210
public Mono<Void> commitTransaction() {
211211
return Mono.defer(() -> {
212-
return QueryFlow.doneTransaction(client, this, true, lockWaitTimeout, batchSupported);
212+
return QueryFlow.doneTransaction(client, this, true, batchSupported);
213213
});
214214
}
215215

@@ -223,19 +223,7 @@ public MySqlBatch createBatch() {
223223
@Override
224224
public Mono<Void> createSavepoint(String name) {
225225
requireValidName(name, "Savepoint name must not be empty and not contain backticks");
226-
227-
String sql = String.format("SAVEPOINT `%s`", name);
228-
229-
return Mono.defer(() -> {
230-
if (isInTransaction()) {
231-
return QueryFlow.executeVoid(client, sql);
232-
} else if (batchSupported) {
233-
// If connection does not in transaction, then starts transaction.
234-
return QueryFlow.executeVoid(client, "BEGIN;" + sql);
235-
}
236-
237-
return QueryFlow.executeVoid(client, "BEGIN", sql);
238-
});
226+
return QueryFlow.createSavepoint(client, this, name, batchSupported);
239227
}
240228

241229
@Override
@@ -286,7 +274,7 @@ public Mono<Void> releaseSavepoint(String name) {
286274
@Override
287275
public Mono<Void> rollbackTransaction() {
288276
return Mono.defer(() -> {
289-
return QueryFlow.doneTransaction(client, this, false, lockWaitTimeout, batchSupported);
277+
return QueryFlow.doneTransaction(client, this, false, batchSupported);
290278
});
291279
}
292280

@@ -371,6 +359,11 @@ public void setIsolationLevel(IsolationLevel level) {
371359
this.currentLevel = level;
372360
}
373361

362+
@Override
363+
public long getSessionLockWaitTimeout() {
364+
return lockWaitTimeout;
365+
}
366+
374367
@Override
375368
public void setCurrentLockWaitTimeout(long timeoutSeconds) {
376369
this.currentLockWaitTimeout = timeoutSeconds;

0 commit comments

Comments
 (0)