Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/com/mongodb/jdbc/MongoConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class MongoConnection implements Connection {
private static Map<String, FileHandler> fileHandlers = new HashMap<String, FileHandler>();
private String logDirPath;
private boolean extJsonMode;
private boolean mongoClientCacheEnabled;
private UuidRepresentation uuidRepresentation;
private String appName;
private MongoSQLTranslate mongosqlTranslate;
Expand Down Expand Up @@ -147,6 +148,7 @@ private void initializeConnection(MongoConnectionProperties connectionProperties
this.user = connectionProperties.getConnectionString().getUsername();
this.currentDB = connectionProperties.getDatabase();
this.extJsonMode = connectionProperties.getExtJsonMode();
this.mongoClientCacheEnabled = connectionProperties.getMongoClientCacheEnabled();
this.uuidRepresentation =
connectionProperties.getConnectionString().getUuidRepresentation();
this.appName = buildAppName(connectionProperties);
Expand Down Expand Up @@ -378,6 +380,11 @@ public void close() {
return;
}

if (!mongoClientCacheEnabled) {
// The MongoClient is owned by this connection.
mongoClient.close();
}

// Decrement fileHandlerCount and delete entry
// if no more connections are using it.
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class MongoConnectionProperties {
private String clientInfo;
private boolean extJsonMode;
private String x509PemPath;
private boolean mongoClientCacheEnabled;

public MongoConnectionProperties(
ConnectionString connectionString,
Expand All @@ -36,14 +37,16 @@ public MongoConnectionProperties(
File logDir,
String clientInfo,
boolean extJsonMode,
String x509PemPath) {
String x509PemPath,
boolean mongoClientCacheEnabled) {
this.connectionString = connectionString;
this.database = database;
this.logLevel = logLevel;
this.logDir = logDir;
this.clientInfo = clientInfo;
this.extJsonMode = extJsonMode;
this.x509PemPath = x509PemPath;
this.mongoClientCacheEnabled = mongoClientCacheEnabled;
}

public ConnectionString getConnectionString() {
Expand Down Expand Up @@ -74,6 +77,10 @@ public String getX509PemPath() {
return x509PemPath;
}

public boolean getMongoClientCacheEnabled() {
return mongoClientCacheEnabled;
}

/*
* Generate a unique key for the connection properties. This key is used to identify the connection properties in the
* connection cache. Properties that do not differentiate a specific client such as the log level are not included in the key.
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/mongodb/jdbc/MongoDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public enum MongoJDBCProperty {
LOG_LEVEL("loglevel"),
LOG_DIR("logdir"),
EXT_JSON_MODE("extjsonmode"),
X509_PEM_PATH("x509pempath");
X509_PEM_PATH("x509pempath"),
MONGO_CLIENT_CACHE_ENABLED("mongoclientcacheenabled");

private final String propertyName;

Expand Down Expand Up @@ -441,6 +442,9 @@ private MongoConnection createConnection(
}
}

String mongoClientCacheEnabledVal = info.getProperty(MONGO_CLIENT_CACHE_ENABLED.getPropertyName(), "true");
boolean mongoClientCacheEnabled = mongoClientCacheEnabledVal.equalsIgnoreCase("true");

MongoConnectionProperties mongoConnectionProperties =
new MongoConnectionProperties(
cs,
Expand All @@ -449,7 +453,12 @@ private MongoConnection createConnection(
logDir,
clientInfo,
extJsonMode,
info.getProperty(X509_PEM_PATH.getPropertyName()));
info.getProperty(X509_PEM_PATH.getPropertyName()),
mongoClientCacheEnabled);

if (!mongoClientCacheEnabled) {
return new MongoConnection(mongoConnectionProperties, x509Passphrase);
}

Integer key = mongoConnectionProperties.generateKey();

Expand Down