Skip to content

Commit b5bbb99

Browse files
committed
adds a closeable extension to MongoClient (fixes #193)
MongoClient provides a close method but does not implement the AutoCloseable interface. And the restx factory during its 'close' process is calling close methods on every components implementing the AutoCloseable interface. So the solution for this problem is to override MongoClient in order to implement the AutoCloseable interface.
1 parent 88e1be3 commit b5bbb99

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package restx.mongo;
2+
3+
import com.mongodb.MongoClient;
4+
import com.mongodb.MongoClientOptions;
5+
import com.mongodb.MongoClientURI;
6+
import com.mongodb.MongoCredential;
7+
import com.mongodb.ServerAddress;
8+
9+
import java.net.UnknownHostException;
10+
import java.util.List;
11+
12+
/**
13+
* Simple extension of the MongoClient in order to add the {@link AutoCloseable} type.
14+
*
15+
* @author apeyrard
16+
*/
17+
public class CloseableMongoClient extends MongoClient implements AutoCloseable {
18+
19+
public CloseableMongoClient() throws UnknownHostException {
20+
}
21+
22+
public CloseableMongoClient(String host) throws UnknownHostException {
23+
super(host);
24+
}
25+
26+
public CloseableMongoClient(String host, MongoClientOptions options) throws UnknownHostException {
27+
super(host, options);
28+
}
29+
30+
public CloseableMongoClient(String host, int port) throws UnknownHostException {
31+
super(host, port);
32+
}
33+
34+
public CloseableMongoClient(ServerAddress addr) {
35+
super(addr);
36+
}
37+
38+
public CloseableMongoClient(ServerAddress addr, List<MongoCredential> credentialsList) {
39+
super(addr, credentialsList);
40+
}
41+
42+
public CloseableMongoClient(ServerAddress addr, MongoClientOptions options) {
43+
super(addr, options);
44+
}
45+
46+
public CloseableMongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options) {
47+
super(addr, credentialsList, options);
48+
}
49+
50+
public CloseableMongoClient(List<ServerAddress> seeds) {
51+
super(seeds);
52+
}
53+
54+
public CloseableMongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList) {
55+
super(seeds, credentialsList);
56+
}
57+
58+
public CloseableMongoClient(List<ServerAddress> seeds, MongoClientOptions options) {
59+
super(seeds, options);
60+
}
61+
62+
public CloseableMongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options) {
63+
super(seeds, credentialsList, options);
64+
}
65+
66+
public CloseableMongoClient(MongoClientURI uri) throws UnknownHostException {
67+
super(uri);
68+
}
69+
}

restx-jongo/src/main/java/restx/mongo/MongoModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class MongoModule {
2727
@Provides @Named(MONGO_CLIENT_NAME)
2828
public MongoClient mongoClient(MongoSettings settings) {
2929
try {
30-
return new MongoClient(new MongoClientURI(settings.uri()));
30+
return new CloseableMongoClient(new MongoClientURI(settings.uri()));
3131
} catch (UnknownHostException e) {
3232
throw new RuntimeException(e);
3333
}

0 commit comments

Comments
 (0)