1  Packagecom.voice.db;2 3 ImportCom.mongodb.DB;4 Importcom.mongodb.DBCollection;5 ImportCom.mongodb.Mongo;6 Importcom.mongodb.MongoClient;7 Importcom.mongodb.MongoClientOptions;8 Importcom.mongodb.ReadPreference;9 Importcom.mongodb.ServerAddress;Ten ImportCom.mongodb.WriteConcern; One  A  -  Public classMongoDB { -  the     PrivateMongo Wmongodb; -      -     Private StaticMongoDB instance =NULL; -      +     Private Static synchronizedMongoDB getinstance () -     { +         if(!isinstancealive ()) A         { atInstance =NewMongoDB (); -         } -         returninstance; -     } -  -     /** in * Determine if the database is in a connected state -      * @returntrue: Connect in <br/> to * false: Disconnected +      */ -     Private Static Booleanisinstancealive () { the         BooleanRetBool =false; *         Try { $             //try to access the database oncePanax NotoginsengDbcollection col = Instance.wMongodb.getDB ("database_name"). GetCollection ("table_name")); - Col.count (); theRetBool =true; +}Catch(Exception e) { A             Try { the instance.wMongodb.close (); +}Catch(Exception ex) {} -         } $         returnRetBool; $     } -      -     PrivateMongoDB () the     { -         Try {WuyiMongoclientoptions.builder Voicedbbuilder =Mongoclientoptions.builder (); theVoicedbbuilder.connecttimeout (3000); -Voicedbbuilder.sockettimeout (6000); WuVoicedbbuilder.autoconnectretry (true); -Voicedbbuilder.connectionsperhost (5); About voicedbbuilder.readpreference (readpreference.secondarypreferred ()); $Voicedbbuilder.socketkeepalive (true); -Mongoclientoptions voicedboptions =voicedbbuilder.build (); -  - //Wmongodb = new Mongoclient (New ServerAddress ("172.16.10.15", 27020), voicedboptions); AWmongodb =NewMongoclient (NewServerAddress ("localhost", 27020), voicedboptions); +              theDB db = Wmongodb.getdb ("Db_name"); - //db db = Wmongodb.getdb ("olacloud_internal"); $Db.authenticate ("id", "id". ToCharArray ()); the Db.setwriteconcern (writeconcern.safe); the}Catch(Exception e) { the             //TODO auto-generated Catch block the e.printstacktrace (); -         } in          the     } the  About     /** the * Get database connection the      * @returndatabases that are already connected the      */ +      Public StaticDB Getdb () { -DB db = Mongodb.getinstance (). Wmongodb.getdb ("Db_name"); the //db db = Mongodb.getinstance (). Wmongodb.getdb ("olacloud_internal");Bayi         returndb; the     } the  -     /** - * Get a table connection the      * @paramtableName Table name the      * @returnTable Connection the      */ the      Public Staticdbcollection getdbcollection (String tableName) { -DB db =Mongodb.getdb (); theDbcollection col =db.getcollection (tableName); the         returnCol; the     }94      the     /** the * Close the current MongoDB connection the      */98      Public Static voidClose () { About         if(Instance! =NULL) { - instance.wMongodb.close ();101         }102     }103 104}
Creation of a MongoDB object