美文网首页
[并发]如果dao层是单例,怎么保证collection的线程安

[并发]如果dao层是单例,怎么保证collection的线程安

作者: 复苏森林 | 来源:发表于2015-11-11 18:40 被阅读335次
       
private static ThreadLocal<DBCollection> collectionHolderLocal = new ThreadLocal<DBCollection>() {  
    @Override
    protected DBCollection initialValue() {  
             return null;
    }
};
    
protected DBCollection collection() throws MMException {
    System.out.println(collectionHolderLocal);
    if(null == collectionHolderLocal.get()) {
        try {
              DBCollection collection = (DBCollection) MongoDBUtil.getDB().getCollection("dbname");
              collectionHolderLocal.set(collection);
        } catch (MMException e) {
              throw new Exception("连接数据库失败" + e.getMessage());
        }
    }
    return collectionHolderLocal.get();
}

相关文章

网友评论

      本文标题:[并发]如果dao层是单例,怎么保证collection的线程安

      本文链接:https://www.haomeiwen.com/subject/dephhttx.html