diff --git a/src/main/java/com/mongodb/jdbc/MongoConnection.java b/src/main/java/com/mongodb/jdbc/MongoConnection.java index 98fd4671..4a26a73e 100644 --- a/src/main/java/com/mongodb/jdbc/MongoConnection.java +++ b/src/main/java/com/mongodb/jdbc/MongoConnection.java @@ -69,6 +69,7 @@ public class MongoConnection implements Connection { private static Map fileHandlers = new HashMap(); private String logDirPath; private boolean extJsonMode; + private boolean mongoClientCacheEnabled; private UuidRepresentation uuidRepresentation; private String appName; private MongoSQLTranslate mongosqlTranslate; @@ -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); @@ -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) { diff --git a/src/main/java/com/mongodb/jdbc/MongoConnectionProperties.java b/src/main/java/com/mongodb/jdbc/MongoConnectionProperties.java index aac44c51..807d42df 100644 --- a/src/main/java/com/mongodb/jdbc/MongoConnectionProperties.java +++ b/src/main/java/com/mongodb/jdbc/MongoConnectionProperties.java @@ -28,6 +28,7 @@ public class MongoConnectionProperties { private String clientInfo; private boolean extJsonMode; private String x509PemPath; + private boolean mongoClientCacheEnabled; public MongoConnectionProperties( ConnectionString connectionString, @@ -36,7 +37,8 @@ public MongoConnectionProperties( File logDir, String clientInfo, boolean extJsonMode, - String x509PemPath) { + String x509PemPath, + boolean mongoClientCacheEnabled) { this.connectionString = connectionString; this.database = database; this.logLevel = logLevel; @@ -44,6 +46,7 @@ public MongoConnectionProperties( this.clientInfo = clientInfo; this.extJsonMode = extJsonMode; this.x509PemPath = x509PemPath; + this.mongoClientCacheEnabled = mongoClientCacheEnabled; } public ConnectionString getConnectionString() { @@ -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. diff --git a/src/main/java/com/mongodb/jdbc/MongoDriver.java b/src/main/java/com/mongodb/jdbc/MongoDriver.java index 5e7821e4..729e1712 100644 --- a/src/main/java/com/mongodb/jdbc/MongoDriver.java +++ b/src/main/java/com/mongodb/jdbc/MongoDriver.java @@ -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; @@ -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, @@ -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();