Skip to content

Commit c1a52de

Browse files
mp911dechristophstrobl
authored andcommitted
Introduce SessionSynchronization.NEVER to disable transactional participation.
SessionSynchronization.NEVER bypasses all transactional integration in cases where applications do not want to make use of transactions so that transaction inspection overhead is avoided. Original Pull Request: #3809
1 parent 7e94c1b commit c1a52de

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoDatabaseUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ private static MongoDatabase doGetMongoDatabase(@Nullable String dbName, MongoDa
104104

105105
Assert.notNull(factory, "Factory must not be null!");
106106

107-
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
107+
if (sessionSynchronization == SessionSynchronization.NEVER
108+
|| !TransactionSynchronizationManager.isSynchronizationActive()) {
108109
return StringUtils.hasText(dbName) ? factory.getMongoDatabase(dbName) : factory.getMongoDatabase();
109110
}
110111

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/ReactiveMongoDatabaseUtils.java

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ private static Mono<MongoDatabase> doGetMongoDatabase(@Nullable String dbName, R
138138

139139
Assert.notNull(factory, "DatabaseFactory must not be null!");
140140

141+
if (sessionSynchronization == SessionSynchronization.NEVER) {
142+
return getMongoDatabaseOrDefault(dbName, factory);
143+
}
144+
141145
return TransactionSynchronizationManager.forCurrentTransaction()
142146
.filter(TransactionSynchronizationManager::isSynchronizationActive) //
143147
.flatMap(synchronizationManager -> {

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/SessionSynchronization.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
*/
1616
package org.springframework.data.mongodb;
1717

18+
import org.springframework.data.mongodb.core.MongoTemplate;
19+
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
20+
1821
/**
19-
* {@link SessionSynchronization} is used along with {@link org.springframework.data.mongodb.core.MongoTemplate} to
20-
* define in which type of transactions to participate if any.
22+
* {@link SessionSynchronization} is used along with {@code MongoTemplate} to define in which type of transactions to
23+
* participate if any.
2124
*
2225
* @author Christoph Strobl
2326
* @author Mark Paluch
2427
* @since 2.1
28+
* @see MongoTemplate#setSessionSynchronization(SessionSynchronization)
29+
* @see MongoDatabaseUtils#getDatabase(MongoDatabaseFactory, SessionSynchronization)
30+
* @see ReactiveMongoTemplate#setSessionSynchronization(SessionSynchronization)
31+
* @see ReactiveMongoDatabaseUtils#getDatabase(ReactiveMongoDatabaseFactory, SessionSynchronization)
2532
*/
2633
public enum SessionSynchronization {
2734

@@ -34,5 +41,12 @@ public enum SessionSynchronization {
3441
/**
3542
* Synchronize with native MongoDB transactions initiated via {@link MongoTransactionManager}.
3643
*/
37-
ON_ACTUAL_TRANSACTION;
44+
ON_ACTUAL_TRANSACTION,
45+
46+
/**
47+
* Do not participate in ongoing transactions.
48+
*
49+
* @since 3.2.5
50+
*/
51+
NEVER;
3852
}

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/MongoDatabaseUtilsUnitTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ void shouldNotStartSessionWhenNoTransactionOngoing() {
109109
verify(dbFactory, never()).withSession(any(ClientSession.class));
110110
}
111111

112+
@Test // GH-3760
113+
void shouldJustReturnDatabaseIfSessionSynchronizationDisabled() throws Exception {
114+
115+
when(dbFactory.getMongoDatabase()).thenReturn(db);
116+
117+
JtaTransactionManager txManager = new JtaTransactionManager(userTransaction);
118+
TransactionTemplate txTemplate = new TransactionTemplate(txManager);
119+
120+
txTemplate.execute(new TransactionCallbackWithoutResult() {
121+
122+
@Override
123+
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
124+
125+
MongoDatabaseUtils.getDatabase(dbFactory, SessionSynchronization.NEVER);
126+
127+
assertThat(TransactionSynchronizationManager.hasResource(dbFactory)).isFalse();
128+
}
129+
});
130+
131+
verify(userTransaction).getStatus();
132+
verifyNoMoreInteractions(userTransaction);
133+
verifyNoInteractions(session);
134+
}
135+
112136
@Test // DATAMONGO-1920
113137
void shouldParticipateInOngoingJtaTransactionWithCommitWhenSessionSychronizationIsAny() throws Exception {
114138

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/ReactiveMongoDatabaseUtilsUnitTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ void isTransactionActiveShouldLookupTxForActiveTransactionSynchronizationViaTxMa
8888
}).as(StepVerifier::create).expectNext(true).verifyComplete();
8989
}
9090

91+
@Test // GH-3760
92+
void shouldJustReturnDatabaseIfSessionSynchronizationDisabled() {
93+
94+
when(databaseFactory.getMongoDatabase()).thenReturn(Mono.just(db));
95+
96+
ReactiveMongoDatabaseUtils.getDatabase(databaseFactory, SessionSynchronization.NEVER) //
97+
.as(StepVerifier::create) //
98+
.expectNextCount(1) //
99+
.verifyComplete();
100+
101+
verify(databaseFactory, never()).getSession(any());
102+
verify(databaseFactory, never()).withSession(any(ClientSession.class));
103+
}
104+
91105
@Test // DATAMONGO-2265
92106
void shouldNotStartSessionWhenNoTransactionOngoing() {
93107

0 commit comments

Comments
 (0)