package cn.cerc.db.mongo;

import cn.cerc.core.IConfig;
import cn.cerc.core.IConnection;
import cn.cerc.db.core.ServerConfig;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MongoConnection implements IConnection, AutoCloseable {

    public static final String mgdb_dbname = "mgdb.dbname";
    public static final String mgdb_username = "mgdb.username";
    public static final String mgdb_password = "mgdb.password";
    public static final String mgdb_site = "mgdb.ipandport";
    public static final String mgdb_enablerep = "mgdb.enablerep";
    public static final String mgdb_replicaset = "mgdb.replicaset";
    public static final String mgdb_maxpoolsize = "mgdb.maxpoolsize";
    public static final String sessionId = "mongoSession";

    private static MongoClient pool;
    private static String dbname;
    private MongoDatabase database;
    private IConfig config;

    public MongoConnection() {
        config = ServerConfig.INSTANCE;
    }

    @Override
    public MongoDatabase getClient() {
        if (database != null) {
            return database;
        }

        if (MongoConnection.pool == null) {
            dbname = config.getProperty(MongoConnection.mgdb_dbname);
            StringBuffer sb = new StringBuffer();
            sb.append("mongodb://");
            // userName
            sb.append(config.getProperty(MongoConnection.mgdb_username));
            // password
            sb.append(":").append(config.getProperty(MongoConnection.mgdb_password));
            // ip
            sb.append("@").append(config.getProperty(MongoConnection.mgdb_site));
            // database
            sb.append("/").append(config.getProperty(MongoConnection.mgdb_dbname));

            if ("true".equals(config.getProperty(MongoConnection.mgdb_enablerep))) {
                // replacaset
                sb.append("?").append("replicaSet=").append(config.getProperty(MongoConnection.mgdb_replicaset));
                // poolsize
                sb.append("&").append("maxPoolSize=").append(config.getProperty(MongoConnection.mgdb_maxpoolsize));
                log.info("连接到MongoDB分片集群:" + sb.toString());
            }
            MongoClientURI connectionString = new MongoClientURI(sb.toString());
            pool = new MongoClient(connectionString);
        }

        database = pool.getDatabase(dbname);

        return database;
    }

    @Override
    public void close() throws Exception {
        if (database != null) {
            database = null;
        }
    }

    @Override
    public String getClientId() {
        return sessionId;
    }

    public IConfig getConfig() {
        return config;
    }

    @Override
    public void setConfig(IConfig config) {
        this.config = config;
    }
}
@@ -2412,24 +2412,182 @@
 poolsize));%0A
+                sb.append(%22&%22).append(%22connectTimeoutMS=%22).append(%223000%22);%0A                sb.append(%22&%22).append(%22serverSelectionTimeoutMS=%22).append(%223000%22);%0A
             
@@ -2627,32 +2627,32 @@
 sb.toString());%0A
-
             %7D%0A  
@@ -2786,33 +2786,32 @@
 ing);%0A        %7D%0A
-%0A
         database
@@ -2839,17 +2839,16 @@
 bname);%0A
-%0A
         
@@ -2912,25 +2912,8 @@
 se()
- throws Exception
  %7B%0A 
@@ -3221,15 +3221,16 @@
 config;%0A
-
     %7D%0A
+%0A
 %7D

Update MongoConnection.java