zoukankan      html  css  js  c++  java
  • 单例模式(Mongo对象的创建)

    单例模式:

      饿汉式单例

    //饿汉式单例类.在类初始化时,已经自行实例化   
    public class Singleton1 {  
        //私有的默认构造子  
        private Singleton1() {}  
        //已经自行实例化   
        private static final Singleton1 single = new Singleton1();  
        //静态工厂方法   
        public static Singleton1 getInstance() {  
            return single;  
        }  
    }

      懒汉式单例

    //懒汉式单例类.在第一次调用的时候实例化   
    public class Singleton2 {  
        //私有的默认构造子  
        private Singleton2() {}  
        //注意,这里没有final      
        private static Singleton2 single=null;  
        //静态工厂方法   
        public synchronized  static Singleton2 getInstance() {  
             if (single == null) {    
                 single = new Singleton2();  
             }    
            return single;  
        }  
    }  

    Mongo对象创建的一个简单单例:

      在单例类中可以使它的属性也成为单例例如本例子中的 wMongodb 在私有的构造函数中初始化该属性即可实现 属性单例化

     1 public class MongoDB {
     2 
     3     private Mongo wMongodb;
     4     
     5     private static MongoDB instance = null;
     6     
     7     private static synchronized MongoDB GetInstance()
     8     {
     9         if(!isInstanceAlive())
    10         {
    11             instance = new MongoDB();
    12         }
    13         return instance;
    14     }
    15 
    16     /**
    17      * 判断数据库是否处于连接状态中
    18      * @return true:连接中<br/>
    19      *        false:已断开
    20      */
    21     private static boolean isInstanceAlive() {
    22         boolean retBool = false;
    23         try {
    24             // 尝试访问一次数据库
    25             DBCollection col = instance.wMongodb.getDB("olacloud").getCollection("noexist");
    26             col.find().count();
    27             retBool = true;
    28         } catch (Exception e) {
    29             try {
    30                 instance.wMongodb.close();
    31             } catch (Exception ex) {}
    32         }
    33         return retBool;
    34     }
    35     
    36     private MongoDB()
    37     {
    38         try {
    39             MongoClientOptions.Builder voicedbBuilder = MongoClientOptions.builder();
    40             voicedbBuilder.connectTimeout(3000);
    41             voicedbBuilder.socketTimeout(6000);
    42             voicedbBuilder.autoConnectRetry(true);
    43             voicedbBuilder.connectionsPerHost(5);
    44             voicedbBuilder.readPreference(ReadPreference.secondaryPreferred());
    45             voicedbBuilder.socketKeepAlive(true);
    46             MongoClientOptions voicedbOptions = voicedbBuilder.build();
    47 
    48 //            wMongodb = new MongoClient(new ServerAddress("172.16.10.15", 27020),voicedbOptions);
    49             wMongodb = new MongoClient(new ServerAddress("wmongo.olavoice.com", 27020),voicedbOptions);
    50             
    51             DB db = wMongodb.getDB("olacloud");
    52 //            DB db = wMongodb.getDB("olacloud_internal");
    53             db.authenticate("olacloud", "olacloud".toCharArray());
    54             db.setWriteConcern(WriteConcern.SAFE);
    55         } catch (Exception e) {
    56             // TODO Auto-generated catch block
    57             e.printStackTrace();
    58         }
    59         
    60     }
    61 
    62     /**
    63      * 获取数据库连接
    64      * @return 已经连接的数据库
    65      */
    66     public static DB getDB() {
    67         DB db = MongoDB.GetInstance().wMongodb.getDB("olacloud");
    68 //        DB db = MongoDB.GetInstance().wMongodb.getDB("olacloud_internal");
    69         return db;
    70     }
    71 
    72     /**
    73      * 获取table的连接
    74      * @param tableName table名
    75      * @return table连接
    76      */
    77     public static DBCollection getDBCollection(String tableName) {
    78         DB db = MongoDB.getDB();
    79         DBCollection col = db.getCollection(tableName);
    80         return col;
    81     }
    82     
    83     /**
    84      * 关闭当前的Mongodb连接
    85      */
    86     public static void close() {
    87         if (instance != null) {
    88             instance.wMongodb.close();
    89         }
    90     }
    91 }
  • 相关阅读:
    POJ 1451
    LightOJ 1224
    POJ 2001
    HDU 2072
    POJ 3764
    CH 1602
    CH 1601
    Gym 101873K
    CH 1201
    Gym 101873C
  • 原文地址:https://www.cnblogs.com/Wen-yu-jing/p/3783430.html
Copyright © 2011-2022 走看看