Skip to content

Commit cc7ae66

Browse files
authored
Support changing of transaction's isolation level via aspect (#126)
Authored-by: Alexander Trunin <[email protected]>
1 parent a63b6e8 commit cc7ae66

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

aspect/src/main/java/tech/ydb/yoj/aspect/tx/YojTransactionAspect.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,28 @@ private Object doInTransaction(ProceedingJoinPoint pjp, YojTransactional transac
4848
if (transactional.maxRetries() != YojTransactional.UNDEFINED) {
4949
localTx = tx.withMaxRetries(transactional.maxRetries());
5050
}
51-
return transactional.readOnly() ? localTx.readOnly().run(() -> safeCall(pjp)) : localTx.tx(() -> safeCall(pjp));
51+
52+
validateIsolationLevel(transactional);
53+
54+
if (transactional.readOnly()) {
55+
return localTx
56+
.readOnly()
57+
.withStatementIsolationLevel(transactional.isolation())
58+
.run(() -> safeCall(pjp));
59+
} else {
60+
return localTx.tx(() -> safeCall(pjp));
61+
}
5262
} catch (CallRetryableException | CallException e) {
5363
throw e.getCause();
5464
}
5565
}
5666

67+
private void validateIsolationLevel(YojTransactional transactional) {
68+
if (transactional.isolation().isReadWrite() && transactional.readOnly()) {
69+
throw new IllegalStateException("Unsupported isolation level for read-only transaction: " + transactional.isolation());
70+
}
71+
}
72+
5773
private static String getSimpleMethod(Signature signature) {
5874
return signature.getDeclaringType().getSimpleName() + "." + signature.getName();
5975
}

aspect/src/main/java/tech/ydb/yoj/aspect/tx/YojTransactional.java

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tech.ydb.yoj.aspect.tx;
22

3+
import tech.ydb.yoj.repository.db.IsolationLevel;
4+
35
import java.lang.annotation.ElementType;
46
import java.lang.annotation.Inherited;
57
import java.lang.annotation.Retention;
@@ -23,6 +25,17 @@
2325
*/
2426
boolean readOnly() default false;
2527

28+
/**
29+
* Indicates isolation level of transaction.
30+
* If transaction marked as read only, then following isolation levels are supported:
31+
* ONLINE_CONSISTENT_READ_ONLY
32+
* ONLINE_INCONSISTENT_READ_ONLY
33+
* STALE_CONSISTENT_READ_ONLY
34+
* SNAPSHOT
35+
* If transaction marked as not read only, isolation level is ignored.
36+
*/
37+
IsolationLevel isolation() default IsolationLevel.ONLINE_CONSISTENT_READ_ONLY;
38+
2639
/**
2740
* Set transaction name otherwise method name will be used as transaction name
2841
*/

0 commit comments

Comments
 (0)