Skip to content

Commit 214e4f4

Browse files
committed
Document client disposal using MongoDatabaseFactory.
Closes #5012
1 parent d147b1f commit 214e4f4

File tree

3 files changed

+28
-99
lines changed

3 files changed

+28
-99
lines changed

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

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.data.mongodb.SessionAwareMethodInterceptor;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.Assert;
25-
import org.springframework.util.ObjectUtils;
2625

2726
import com.mongodb.ClientSessionOptions;
2827
import com.mongodb.WriteConcern;
@@ -32,8 +31,7 @@
3231

3332
/**
3433
* Common base class for usage with both {@link com.mongodb.client.MongoClients} defining common properties such as
35-
* database name and exception translator. <br />
36-
* Not intended to be used directly.
34+
* database name and exception translator. Not intended to be used directly.
3735
*
3836
* @author Christoph Strobl
3937
* @author Mark Paluch
@@ -125,6 +123,7 @@ public MongoDatabase getMongoDatabase(String dbName) throws DataAccessException
125123
*/
126124
protected abstract MongoDatabase doGetMongoDatabase(String dbName);
127125

126+
128127
public void destroy() throws Exception {
129128
if (mongoInstanceCreated) {
130129
closeClient();
@@ -162,15 +161,8 @@ protected String getDefaultDatabaseName() {
162161
* @author Christoph Strobl
163162
* @since 2.1
164163
*/
165-
static final class ClientSessionBoundMongoDbFactory implements MongoDatabaseFactory {
166-
167-
private final ClientSession session;
168-
private final MongoDatabaseFactory delegate;
169-
170-
public ClientSessionBoundMongoDbFactory(ClientSession session, MongoDatabaseFactory delegate) {
171-
this.session = session;
172-
this.delegate = delegate;
173-
}
164+
record ClientSessionBoundMongoDbFactory(ClientSession session,
165+
MongoDatabaseFactory delegate) implements MongoDatabaseFactory {
174166

175167
@Override
176168
public MongoDatabase getMongoDatabase() throws DataAccessException {
@@ -199,7 +191,7 @@ public MongoDatabaseFactory withSession(ClientSession session) {
199191

200192
@Override
201193
public boolean isTransactionActive() {
202-
return session != null && session.hasActiveTransaction();
194+
return session.hasActiveTransaction();
203195
}
204196

205197
private MongoDatabase proxyMongoDatabase(MongoDatabase database) {
@@ -228,39 +220,6 @@ private <T> T createProxyInstance(com.mongodb.session.ClientSession session, T t
228220
return targetType.cast(factory.getProxy(target.getClass().getClassLoader()));
229221
}
230222

231-
public ClientSession getSession() {
232-
return this.session;
233-
}
234-
235-
public MongoDatabaseFactory getDelegate() {
236-
return this.delegate;
237-
}
238-
239-
@Override
240-
public boolean equals(@Nullable Object o) {
241-
if (this == o)
242-
return true;
243-
if (o == null || getClass() != o.getClass())
244-
return false;
245-
246-
ClientSessionBoundMongoDbFactory that = (ClientSessionBoundMongoDbFactory) o;
247-
248-
if (!ObjectUtils.nullSafeEquals(this.session, that.session)) {
249-
return false;
250-
}
251-
return ObjectUtils.nullSafeEquals(this.delegate, that.delegate);
252-
}
253-
254-
@Override
255-
public int hashCode() {
256-
int result = ObjectUtils.nullSafeHashCode(this.session);
257-
result = 31 * result + ObjectUtils.nullSafeHashCode(this.delegate);
258-
return result;
259-
}
260-
261-
public String toString() {
262-
return "MongoDatabaseFactorySupport.ClientSessionBoundMongoDbFactory(session=" + this.getSession() + ", delegate="
263-
+ this.getDelegate() + ")";
264-
}
265223
}
224+
266225
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public class SimpleMongoClientDatabaseFactory extends MongoDatabaseFactorySuppor
3434
implements DisposableBean {
3535

3636
/**
37-
* Creates a new {@link SimpleMongoClientDatabaseFactory} instance for the given {@code connectionString}.
37+
* Creates a new {@link SimpleMongoClientDatabaseFactory} instance for the given {@code connectionString}. Using this
38+
* constructor will create a new {@link MongoClient} instance that will be closed when calling {@link #destroy()}.
3839
*
3940
* @param connectionString connection coordinates for a database connection. Must contain a database name and must not
4041
* be {@literal null} or empty.
@@ -45,7 +46,8 @@ public SimpleMongoClientDatabaseFactory(String connectionString) {
4546
}
4647

4748
/**
48-
* Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}.
49+
* Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}. Using this
50+
* constructor will create a new {@link MongoClient} instance that will be closed when calling {@link #destroy()}.
4951
*
5052
* @param connectionString connection coordinates for a database connection. Must contain also a database name and not
5153
* be {@literal null}.
@@ -55,7 +57,10 @@ public SimpleMongoClientDatabaseFactory(ConnectionString connectionString) {
5557
}
5658

5759
/**
58-
* Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}.
60+
* Creates a new {@link SimpleMongoClientDatabaseFactory} instance from the given {@link MongoClient}. Note that the
61+
* client will not be closed when calling {@link #destroy()} as we assume a managed client instance that we do not
62+
* want to close on {@link #destroy()} meaning that you (or the application container) must dispose the client
63+
* instance once it is no longer required for use.
5964
*
6065
* @param mongoClient must not be {@literal null}.
6166
* @param databaseName must not be {@literal null} or empty.
@@ -89,4 +94,5 @@ protected void closeClient() {
8994
protected MongoDatabase doGetMongoDatabase(String dbName) {
9095
return getMongoClient().getDatabase(dbName);
9196
}
97+
9298
}

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

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.data.mongodb.SessionAwareMethodInterceptor;
2727
import org.springframework.lang.Nullable;
2828
import org.springframework.util.Assert;
29-
import org.springframework.util.ObjectUtils;
3029

3130
import com.mongodb.ClientSessionOptions;
3231
import com.mongodb.ConnectionString;
@@ -55,7 +54,9 @@ public class SimpleReactiveMongoDatabaseFactory implements DisposableBean, React
5554
private @Nullable WriteConcern writeConcern;
5655

5756
/**
58-
* Creates a new {@link SimpleReactiveMongoDatabaseFactory} instance from the given {@link ConnectionString}.
57+
* Creates a new {@link SimpleReactiveMongoDatabaseFactory} instance from the given {@link ConnectionString}. Using
58+
* this constructor will create a new {@link MongoClient} instance that will be closed when calling
59+
* {@link #destroy()}.
5960
*
6061
* @param connectionString must not be {@literal null}.
6162
*/
@@ -64,7 +65,10 @@ public SimpleReactiveMongoDatabaseFactory(ConnectionString connectionString) {
6465
}
6566

6667
/**
67-
* Creates a new {@link SimpleReactiveMongoDatabaseFactory} instance from the given {@link MongoClient}.
68+
* Creates a new {@link SimpleReactiveMongoDatabaseFactory} instance from the given {@link MongoClient}. Note that the
69+
* client will not be closed when calling {@link #destroy()} as we assume a managed client instance that we do not
70+
* want to close on {@link #destroy()} meaning that you (or the application container) must dispose the client
71+
* instance once it is no longer required for use.
6872
*
6973
* @param mongoClient must not be {@literal null}.
7074
* @param databaseName must not be {@literal null}.
@@ -163,16 +167,8 @@ public ReactiveMongoDatabaseFactory withSession(ClientSession session) {
163167
* @author Christoph Strobl
164168
* @since 2.1
165169
*/
166-
static final class ClientSessionBoundMongoDbFactory implements ReactiveMongoDatabaseFactory {
167-
168-
private final ClientSession session;
169-
private final ReactiveMongoDatabaseFactory delegate;
170-
171-
ClientSessionBoundMongoDbFactory(ClientSession session, ReactiveMongoDatabaseFactory delegate) {
172-
173-
this.session = session;
174-
this.delegate = delegate;
175-
}
170+
record ClientSessionBoundMongoDbFactory(ClientSession session,
171+
ReactiveMongoDatabaseFactory delegate) implements ReactiveMongoDatabaseFactory {
176172

177173
@Override
178174
public Mono<MongoDatabase> getMongoDatabase() throws DataAccessException {
@@ -206,7 +202,7 @@ public ReactiveMongoDatabaseFactory withSession(ClientSession session) {
206202

207203
@Override
208204
public boolean isTransactionActive() {
209-
return session != null && session.hasActiveTransaction();
205+
return session.hasActiveTransaction();
210206
}
211207

212208
private MongoDatabase decorateDatabase(MongoDatabase database) {
@@ -217,7 +213,8 @@ private MongoDatabase proxyDatabase(com.mongodb.session.ClientSession session, M
217213
return createProxyInstance(session, database, MongoDatabase.class);
218214
}
219215

220-
private MongoCollection proxyCollection(com.mongodb.session.ClientSession session, MongoCollection collection) {
216+
private MongoCollection<?> proxyCollection(com.mongodb.session.ClientSession session,
217+
MongoCollection<?> collection) {
221218
return createProxyInstance(session, collection, MongoCollection.class);
222219
}
223220

@@ -234,39 +231,6 @@ private <T> T createProxyInstance(com.mongodb.session.ClientSession session, T t
234231
return targetType.cast(factory.getProxy(target.getClass().getClassLoader()));
235232
}
236233

237-
public ClientSession getSession() {
238-
return this.session;
239-
}
240-
241-
public ReactiveMongoDatabaseFactory getDelegate() {
242-
return this.delegate;
243-
}
244-
245-
@Override
246-
public boolean equals(@Nullable Object o) {
247-
if (this == o)
248-
return true;
249-
if (o == null || getClass() != o.getClass())
250-
return false;
251-
252-
ClientSessionBoundMongoDbFactory that = (ClientSessionBoundMongoDbFactory) o;
253-
254-
if (!ObjectUtils.nullSafeEquals(this.session, that.session)) {
255-
return false;
256-
}
257-
return ObjectUtils.nullSafeEquals(this.delegate, that.delegate);
258-
}
259-
260-
@Override
261-
public int hashCode() {
262-
int result = ObjectUtils.nullSafeHashCode(this.session);
263-
result = 31 * result + ObjectUtils.nullSafeHashCode(this.delegate);
264-
return result;
265-
}
266-
267-
public String toString() {
268-
return "SimpleReactiveMongoDatabaseFactory.ClientSessionBoundMongoDbFactory(session=" + this.getSession()
269-
+ ", delegate=" + this.getDelegate() + ")";
270-
}
271234
}
235+
272236
}

0 commit comments

Comments
 (0)