Creation of a MongoDB object

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.