Skip to content

Commit f3115e9

Browse files
authored
Merge branch 'main' into feat/basic-lock-cas
2 parents e07f66b + 7417aff commit f3115e9

14 files changed

Lines changed: 1129 additions & 43 deletions

File tree

‎jans-cedarling/flask-sidecar/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies = [
2121
"python-json-logger>=2.0.7,<3",
2222
"gunicorn>=22.0.0,<23",
2323
"orjson>=3.11.5,<4",
24-
"cedarling-python>=2.5.21",
24+
"cedarling-python>=2.5.22",
2525
]
2626

2727
[dependency-groups]

‎jans-cedarling/flask-sidecar/uv.lock‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎jans-orm/core/src/main/java/io/jans/orm/PersistenceEntryManager.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,53 @@ default void persistBatch(List<?> entries) {
161161
"Atomic batch insert (persistBatch) is not supported by '%s' persistence backend", getPersistenceType()));
162162
}
163163

164+
/**
165+
* Reports whether {@link #executeInTransaction(String, TransactionCallback)} and
166+
* {@link #findForUpdate(Class, Object)} are supported for entries stored under {@code primaryKey}.
167+
* Only the SQL backend supports them; a hybrid entry manager returns the capability of the backend
168+
* that the DN maps to.
169+
*
170+
* @param primaryKey DN (or base DN) of the entries the transaction will read and write
171+
* @return {@code true} when transactions are supported for that DN
172+
*/
173+
default boolean isTransactionSupported(String primaryKey) {
174+
return false;
175+
}
176+
177+
/**
178+
* Runs {@code callback} in ONE database transaction. Every read and write this entry manager performs
179+
* on the calling thread while the callback runs joins the transaction. The transaction commits when
180+
* the callback returns normally and rolls back when it throws; the exception is rethrown unchanged
181+
* when it is a {@link RuntimeException}, otherwise it is wrapped in an
182+
* {@link io.jans.orm.exception.EntryPersistenceException}.
183+
*
184+
* <p>A nested call on the same thread joins the outer transaction (no savepoints): the outer call
185+
* decides commit or rollback. Writes that a hybrid entry manager routes to a different backend than
186+
* {@code primaryKey} are not part of the transaction.</p>
187+
*
188+
* @param primaryKey DN (or base DN) that selects the backend
189+
* @param callback unit of work
190+
* @return result of the callback
191+
* @throws io.jans.orm.exception.UnsupportedOperationException the backend has no transactions
192+
*/
193+
default <T> T executeInTransaction(String primaryKey, TransactionCallback<T> callback) {
194+
throw new io.jans.orm.exception.UnsupportedOperationException(String.format(
195+
"Transactions are not supported by '%s' persistence backend", getPersistenceType()));
196+
}
197+
198+
/**
199+
* Reads one entry and locks its row until the current transaction ends ({@code SELECT ... FOR UPDATE}).
200+
* Must be called inside {@link #executeInTransaction(String, TransactionCallback)}.
201+
*
202+
* @return the entry, or {@code null} when it does not exist
203+
* @throws io.jans.orm.exception.UnsupportedOperationException the backend has no row locks
204+
* @throws IllegalStateException called outside a transaction
205+
*/
206+
default <T> T findForUpdate(Class<T> entryClass, Object primaryKey) {
207+
throw new io.jans.orm.exception.UnsupportedOperationException(String.format(
208+
"Row locks are not supported by '%s' persistence backend", getPersistenceType()));
209+
}
210+
164211
/**
165212
* Reads entries by DN. Backends with a native implementation (SQL) read them in one round trip
166213
* per table; the default implementation works on every backend and reads one DN at a time, so
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
3+
*
4+
* Copyright (c) 2026, Janssen Project
5+
*/
6+
7+
package io.jans.orm;
8+
9+
/**
10+
* Unit of work run by {@link PersistenceEntryManager#executeInTransaction(String, TransactionCallback)}.
11+
*
12+
* @param <T> result type
13+
*/
14+
@FunctionalInterface
15+
public interface TransactionCallback<T> {
16+
17+
T doInTransaction() throws Exception;
18+
19+
}

‎jans-orm/core/src/test/java/io/jans/orm/impl/PersistenceEntryManagerBatchDefaultsTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public void persistBatchThrowsUnsupportedByDefaultForEmptyList() {
6666
assertThrows(UnsupportedOperationException.class, () -> entryManager.persistBatch(Collections.emptyList()));
6767
}
6868

69+
@Test
70+
public void transactionsAreNotSupportedByDefault() {
71+
FakeVersionEntryManager entryManager = new FakeVersionEntryManager();
72+
73+
assertFalse(entryManager.isTransactionSupported("ou=people,o=jans"));
74+
assertThrows(UnsupportedOperationException.class,
75+
() -> entryManager.executeInTransaction("ou=people,o=jans", () -> "x"));
76+
assertThrows(UnsupportedOperationException.class,
77+
() -> entryManager.findForUpdate(BatchTestEntry.class, "uid=a,ou=people,o=jans"));
78+
}
79+
6980
@Test
7081
public void findByDnsReturnsFoundEntriesAndOmitsMissingOnes() {
7182
FakeVersionEntryManager entryManager = new FakeVersionEntryManager();

‎jans-orm/hybrid/src/main/java/io/jans/orm/hybrid/impl/HybridEntryManager.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.LoggerFactory;
2222

2323
import io.jans.orm.PersistenceEntryManager;
24+
import io.jans.orm.TransactionCallback;
2425
import io.jans.orm.event.DeleteNotifier;
2526
import io.jans.orm.exception.InvalidArgumentException;
2627
import io.jans.orm.exception.KeyConversionException;
@@ -447,6 +448,28 @@ public boolean isAtomicBatchSupported(String primaryKey) {
447448
return persistenceEntryManager.isAtomicBatchSupported(primaryKey);
448449
}
449450

451+
@Override
452+
public boolean isTransactionSupported(String primaryKey) {
453+
PersistenceEntryManager persistenceEntryManager = getEntryManagerForDn(primaryKey);
454+
return persistenceEntryManager.isTransactionSupported(primaryKey);
455+
}
456+
457+
/**
458+
* Delegates the transaction to the backend of {@code primaryKey}. Only writes that map to the same
459+
* backend join it: one transaction cannot span several databases.
460+
*/
461+
@Override
462+
public <T> T executeInTransaction(String primaryKey, TransactionCallback<T> callback) {
463+
PersistenceEntryManager persistenceEntryManager = getEntryManagerForDn(primaryKey);
464+
return persistenceEntryManager.executeInTransaction(primaryKey, callback);
465+
}
466+
467+
@Override
468+
public <T> T findForUpdate(Class<T> entryClass, Object primaryKey) {
469+
PersistenceEntryManager persistenceEntryManager = getEntryManagerForDn(primaryKey);
470+
return persistenceEntryManager.findForUpdate(entryClass, primaryKey);
471+
}
472+
450473
/**
451474
* Delegates the batch to the backend of the entries' DNs. All entries must map to the same backend:
452475
* one transaction cannot span several databases.

‎jans-orm/hybrid/src/test/java/io/jans/orm/hybrid/impl/HybridEntryManagerBatchTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.Test;
3030

3131
import io.jans.orm.PersistenceEntryManager;
32+
import io.jans.orm.TransactionCallback;
3233
import io.jans.orm.exception.EntryPersistenceException;
3334
import io.jans.orm.exception.InvalidArgumentException;
3435
import io.jans.orm.exception.MappingException;
@@ -76,6 +77,32 @@ public void isAtomicBatchSupportedForDnReportsCapabilityOfResolvedDelegate() {
7677
assertFalse(hybridEntryManager.isAtomicBatchSupported("uid=t,ou=tokens,o=jans"));
7778
}
7879

80+
@Test
81+
public void isTransactionSupportedReportsCapabilityOfResolvedDelegate() {
82+
when(sqlDelegate.isTransactionSupported("uid=a,ou=people,o=jans")).thenReturn(true);
83+
when(couchbaseDelegate.isTransactionSupported("uid=t,ou=tokens,o=jans")).thenReturn(false);
84+
85+
assertTrue(hybridEntryManager.isTransactionSupported("uid=a,ou=people,o=jans"));
86+
assertFalse(hybridEntryManager.isTransactionSupported("uid=t,ou=tokens,o=jans"));
87+
}
88+
89+
@Test
90+
public void executeInTransactionDelegatesToBackendOfPrimaryKey() {
91+
TransactionCallback<String> callback = () -> "x";
92+
when(sqlDelegate.executeInTransaction("ou=people,o=jans", callback)).thenReturn("done");
93+
94+
assertEquals("done", hybridEntryManager.executeInTransaction("ou=people,o=jans", callback));
95+
verify(couchbaseDelegate, never()).executeInTransaction(any(), any());
96+
}
97+
98+
@Test
99+
public void findForUpdateDelegatesToBackendOfPrimaryKey() {
100+
HybridTestEntry found = entry("uid=a,ou=people,o=jans");
101+
when(sqlDelegate.findForUpdate(HybridTestEntry.class, "uid=a,ou=people,o=jans")).thenReturn(found);
102+
103+
assertSame(found, hybridEntryManager.findForUpdate(HybridTestEntry.class, "uid=a,ou=people,o=jans"));
104+
}
105+
79106
@Test
80107
public void isAtomicBatchSupportedIsTrueOnlyWhenEveryDelegateSupportsIt() {
81108
when(sqlDelegate.isAtomicBatchSupported()).thenReturn(true);

0 commit comments

Comments
 (0)