@@ -177,9 +177,10 @@ private String buildKey(Map<String, Object> val, List<String> equalFieldList) {
177177 return sb .toString ();
178178 }
179179
180- private MongoCollection getConn (String address , String database , String tableName ) {
180+ private MongoCollection getConn (String host , String userName , String password , String database , String tableName ) {
181+
181182 MongoCollection dbCollection ;
182- mongoClient = new MongoClient (new MongoClientURI (address ));
183+ mongoClient = new MongoClient (new MongoClientURI (getConnectionUrl ( host , userName , password ) ));
183184 db = mongoClient .getDatabase (database );
184185 dbCollection = db .getCollection (tableName , Document .class );
185186 return dbCollection ;
@@ -193,7 +194,8 @@ private void loadData(Map<String, List<Map<String, Object>>> tmpCache) throws SQ
193194 try {
194195 for (int i = 0 ; i < CONN_RETRY_NUM ; i ++) {
195196 try {
196- dbCollection = getConn (tableInfo .getAddress (), tableInfo .getDatabase (), tableInfo .getTableName ());
197+ dbCollection = getConn (tableInfo .getAddress (), tableInfo .getUserName (), tableInfo .getPassword (),
198+ tableInfo .getDatabase (), tableInfo .getTableName ());
197199 break ;
198200 } catch (Exception e ) {
199201 if (i == CONN_RETRY_NUM - 1 ) {
@@ -251,4 +253,14 @@ private void loadData(Map<String, List<Map<String, Object>>> tmpCache) throws SQ
251253 }
252254 }
253255 }
256+ private String getConnectionUrl (String address , String userName , String password ){
257+ if (address .startsWith ("mongodb://" ) || address .startsWith ("mongodb+srv://" )){
258+ return address ;
259+ }
260+ if (StringUtils .isNotBlank (userName ) && StringUtils .isNotBlank (password )) {
261+ return String .format ("mongodb://%s:%s@%s" , userName , password , address );
262+ }
263+ return String .format ("mongodb://%s" , address );
264+ }
265+
254266}
0 commit comments