Skip to content

Commit 17976c0

Browse files
author
Alexander Lavrukov
committed
new-tx-i: new Tx interface pattern
1 parent 51c3007 commit 17976c0

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.google.common.collect.ImmutableSet;
44
import com.google.common.collect.Iterators;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
7+
import lombok.RequiredArgsConstructor;
58
import org.assertj.core.api.Assertions;
69
import org.junit.Assert;
710
import org.junit.Test;
@@ -288,13 +291,45 @@ public void deferFinallyRollbackRetryable() {
288291

289292
@Test
290293
public void deferFinallyNotInTxContext() {
291-
db.tx(() -> Tx.Current.get().deferFinally(() -> assertFalse(Tx.Current.exists())));
294+
db.txC(tx -> tx.deferFinally(() -> assertFalse(Tx.Current.exists())));
295+
}
296+
297+
@Test
298+
public void myTest() {
299+
db.txC(tx -> {
300+
// new. Usefull because you see that table depends on tx;
301+
Db1.project(tx).findAll();
302+
303+
// new. Usefull because you can initialize db one time in tx and use call table without pass tx any time
304+
Db2.of(tx).project().findAll();
305+
306+
// old
307+
db.projects().findAll();
308+
});
309+
}
310+
311+
// Examples of new DB patterns. User can use one or both;
312+
313+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
314+
final static class Db1 {
315+
public static TestEntityOperations.ProjectTable project(Tx tx) {
316+
return new TestEntityOperations.ProjectTable(tx.table(Project.class));
317+
}
318+
}
319+
320+
@RequiredArgsConstructor(staticName = "of")
321+
final static class Db2 {
322+
private final Tx tx;
323+
324+
TestEntityOperations.ProjectTable project() {
325+
return new TestEntityOperations.ProjectTable(tx.table(Project.class));
326+
}
292327
}
293328

294329
@Test
295330
public void deferFinallyRollbackNotInTxContext() {
296-
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> db.tx(() -> {
297-
Tx.Current.get().deferFinally(() -> assertFalse(Tx.Current.exists()));
331+
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> db.txC(tx -> {
332+
tx.deferFinally(() -> assertFalse(Tx.Current.exists()));
298333
throw new RuntimeException();
299334
}));
300335
}

repository/src/main/java/tech/ydb/yoj/repository/db/Tx.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import java.util.function.Supplier;
77

88
public interface Tx {
9+
default <T extends Entity<T>> Table<T> table(Class<T> cls) {
10+
return getRepositoryTransaction().table(cls);
11+
}
12+
913
void defer(Runnable runnable);
1014

1115
void deferFinally(Runnable runnable);

repository/src/main/java/tech/ydb/yoj/repository/db/TxManager.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import tech.ydb.yoj.repository.db.exception.RetryableException;
88

99
import java.time.Duration;
10+
import java.util.function.Consumer;
11+
import java.util.function.Function;
1012
import java.util.function.Supplier;
1113

1214
public interface TxManager {
@@ -137,7 +139,9 @@ default TxManager noLogging() {
137139
* @param supplier action to perform
138140
* @return action result
139141
*/
140-
<T> T tx(Supplier<T> supplier);
142+
default <T> T tx(Supplier<T> supplier) {
143+
return txF(tx -> supplier.get());
144+
}
141145

142146
/**
143147
* Performs the specified action inside a transaction. The action must be idempotent, because it might be executed
@@ -146,7 +150,18 @@ default TxManager noLogging() {
146150
*
147151
* @param runnable action to perform
148152
*/
149-
void tx(Runnable runnable);
153+
default void tx(Runnable runnable) {
154+
txC(tx -> runnable.run());
155+
}
156+
157+
<T> T txF(Function<Tx, T> supplier);
158+
159+
default void txC(Consumer<Tx> consumer) {
160+
txF(tx -> {
161+
consumer.accept(tx);
162+
return null;
163+
});
164+
}
150165

151166
/**
152167
* Start a transaction-like session of read-only statements. Each statement will be executed <em>separately</em>,

0 commit comments

Comments
 (0)