Skip to content

Commit a4c5682

Browse files
committed
DATAMONGO-2130 - Polishing.
Replace duplicate checks to ClientSession.hasActiveTransaction() with MongoResourceHolder.hasActiveTransaction(). Introduce MongoResourceHolder.getRequiredSession() to avoid nullability warnings. Original pull request: #618.
1 parent b2d2c12 commit a4c5682

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ public static boolean isTransactionActive(MongoDbFactory dbFactory) {
134134
}
135135

136136
MongoResourceHolder resourceHolder = (MongoResourceHolder) TransactionSynchronizationManager.getResource(dbFactory);
137-
return resourceHolder != null
138-
&& (resourceHolder.hasSession() && resourceHolder.getSession().hasActiveTransaction());
137+
return resourceHolder != null && resourceHolder.hasActiveTransaction();
139138
}
140139

141140
@Nullable
@@ -160,7 +159,7 @@ private static ClientSession doGetSession(MongoDbFactory dbFactory, SessionSynch
160159
// init a non native MongoDB transaction by registering a MongoSessionSynchronization
161160

162161
resourceHolder = new MongoResourceHolder(createClientSession(dbFactory), dbFactory);
163-
resourceHolder.getSession().startTransaction();
162+
resourceHolder.getRequiredSession().startTransaction();
164163

165164
TransactionSynchronizationManager
166165
.registerSynchronization(new MongoSessionSynchronization(resourceHolder, dbFactory));
@@ -207,8 +206,8 @@ protected boolean shouldReleaseBeforeCompletion() {
207206
@Override
208207
protected void processResourceAfterCommit(MongoResourceHolder resourceHolder) {
209208

210-
if (isTransactionActive(resourceHolder)) {
211-
resourceHolder.getSession().commitTransaction();
209+
if (resourceHolder.hasActiveTransaction()) {
210+
resourceHolder.getRequiredSession().commitTransaction();
212211
}
213212
}
214213

@@ -219,8 +218,8 @@ protected void processResourceAfterCommit(MongoResourceHolder resourceHolder) {
219218
@Override
220219
public void afterCompletion(int status) {
221220

222-
if (status == TransactionSynchronization.STATUS_ROLLED_BACK && isTransactionActive(this.resourceHolder)) {
223-
resourceHolder.getSession().abortTransaction();
221+
if (status == TransactionSynchronization.STATUS_ROLLED_BACK && this.resourceHolder.hasActiveTransaction()) {
222+
resourceHolder.getRequiredSession().abortTransaction();
224223
}
225224

226225
super.afterCompletion(status);
@@ -234,17 +233,8 @@ public void afterCompletion(int status) {
234233
protected void releaseResource(MongoResourceHolder resourceHolder, Object resourceKey) {
235234

236235
if (resourceHolder.hasActiveSession()) {
237-
resourceHolder.getSession().close();
236+
resourceHolder.getRequiredSession().close();
238237
}
239238
}
240-
241-
private boolean isTransactionActive(MongoResourceHolder resourceHolder) {
242-
243-
if (!resourceHolder.hasSession()) {
244-
return false;
245-
}
246-
247-
return resourceHolder.getSession().hasActiveTransaction();
248-
}
249239
}
250240
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoResourceHolder.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* <strong>Note:</strong> Intended for internal usage only.
2929
*
3030
* @author Christoph Strobl
31+
* @author Mark Paluch
3132
* @since 2.1
3233
* @see MongoTransactionManager
3334
* @see org.springframework.data.mongodb.core.MongoTemplate
@@ -57,6 +58,22 @@ ClientSession getSession() {
5758
return session;
5859
}
5960

61+
/**
62+
* @return the required associated {@link ClientSession}.
63+
* @throws IllegalStateException if no {@link ClientSession} is associated with this {@link MongoResourceHolder}.
64+
* @since 2.1.3
65+
*/
66+
ClientSession getRequiredSession() {
67+
68+
ClientSession session = getSession();
69+
70+
if (session == null) {
71+
throw new IllegalStateException("No session available!");
72+
}
73+
74+
return session;
75+
}
76+
6077
/**
6178
* @return the associated {@link MongoDbFactory}.
6279
*/
@@ -101,7 +118,21 @@ boolean hasActiveSession() {
101118
return false;
102119
}
103120

104-
return hasServerSession() && !getSession().getServerSession().isClosed();
121+
return hasServerSession() && !getRequiredSession().getServerSession().isClosed();
122+
}
123+
124+
/**
125+
* @return {@literal true} if the session has an active transaction.
126+
* @since 2.1.3
127+
* @see #hasActiveSession()
128+
*/
129+
boolean hasActiveTransaction() {
130+
131+
if (!hasActiveSession()) {
132+
return false;
133+
}
134+
135+
return getRequiredSession().hasActiveTransaction();
105136
}
106137

107138
/**
@@ -111,7 +142,7 @@ boolean hasActiveSession() {
111142
boolean hasServerSession() {
112143

113144
try {
114-
return getSession().getServerSession() != null;
145+
return getRequiredSession().getServerSession() != null;
115146
} catch (IllegalStateException serverSessionClosed) {
116147
// ignore
117148
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,11 +1124,11 @@ public long count(Query query, @Nullable Class<?> entityClass, String collection
11241124

11251125
protected long doCount(String collectionName, Document filter, CountOptions options) {
11261126

1127-
if (!MongoDatabaseUtils.isTransactionActive(getMongoDbFactory())) {
1128-
return execute(collectionName, collection -> collection.count(filter, options));
1127+
if (MongoDatabaseUtils.isTransactionActive(getMongoDbFactory())) {
1128+
return execute(collectionName, collection -> collection.countDocuments(filter, options));
11291129
}
11301130

1131-
return execute(collectionName, collection -> collection.countDocuments(filter, options));
1131+
return execute(collectionName, collection -> collection.count(filter, options));
11321132
}
11331133

11341134
/*

0 commit comments

Comments
 (0)