`
BigCat2013
  • 浏览: 52707 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MongoDB的连接池和连接管理

 
阅读更多

在关系型数据库中,我们总是需要关闭使用的数据库连接,不然大量的创建连接会导致资源的浪费甚至于数据库宕机。这篇文章主要想解释一下mongoDB的连接池以及连接管理机制,如果正对此有疑惑的朋友可以看一下。

通常我们习惯于new 一个connection并且通常在finally语句中调用connection的close()方法将其关闭。正巧,mongoDB中当我们new一个Mongo的时候,会发现它也有一个close()方法。所以会出现这样的情况:我们在需要DB操作的方法中new一个mongo实例,然后调用mongo.getDB()方法拿到对应的连接,操作完数据之后再调用mongo.close()方法来关闭连接。 看起来貌似是没有什么问题,但是如果你再研究一下mongo的API,你会发现这样耳朵操作就相当于园丁在浇花的时候去打了一桶水,然后舀了一勺水浇一朵花,然后他把一桶水全倒了回去,重新打一桶水,再舀了一勺水浇另外一朵花。。。

说到这里大家应该都已经明白了,其实当你new Mongo()的时候,就创建了一个连接池,getDB()只是从这个连接池中拿一个可用的连接。而连接池是不需要我们及时关闭的,我们可以在程序的生命周期中维护一个这样的单例,至于从连接池中拿出的连接,我们需要关闭吗?答案是NO。你会发现DB根本没有close()之类的方法。在mongoDB中,一个连接池会维持一定数目的连接,当你需要的时候调用getDB()去连接池中拿到连接,而mongo会在这个DB执行完数据操作时候自动收回连接到连接池中待用。所以在mongoDB中大家不必担心连接没有关闭的问题,在你需要在所有操作结束或者整个程序shutdown的时候调用mongo的close()方法即可。

以下的官网的一些解释:

public Class Mongo:

A database connection with internal connection pooling. For most applications, you should have one Mongo instance for the entire JVM.

public Class MongoClient:

A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM.

Note: This class supersedes the Mongo class. While it extends Mongo, it differs from it in that the default write concern is to wait for acknowledgment from the server of all write operations. In addition, its constructors accept instances of MongoClientOptions and MongoClientURI, which both also set the same default write concern.

In general, users of this class will pick up all of the default options specified in MongoClientOptions. In particular, note that the default value of the connectionsPerHost option has been increased to 100 from the old default value of 10 used by the superceded Mongo class.

 

Mongo 是一个过期的类,取而代之的是MongoClient, 值得一提的是MongoClient把connection的默认值从以前的10个变成了现在的100个,省去了自定义配置的繁琐,很贴心。

 

下面是我写的一个MongoConnectionFactory:

 

 

public class MongoConnFactory {

	private static MongoClient mongoClient = null;

	@SuppressWarnings("deprecation")
	public static  DB getDB() throws UnknownHostException {
		
		 DB conn = null;
		 if(mongoClient == null){
		     intializeMongoClient();
				
		}
		 String dbName = AppConfig.getValue(Const.MONGODB_DBNAME);
		 String username = AppConfig.getValue(Const.MONGODB_USERNAME);
		 String password = AppConfig.getValue(Const.MONGODB_PASSWORD);
		 conn = mongoClient.getDB(dbName);
		 conn.authenticate(username, password.toCharArray());
		 return conn;
			
		
	}

       private static void intializeMongoClient() throws UnknownHostException {
		
	       String host = AppConfig.getValue(Const.MONGODB_HOST);
		   int port = AppConfig.getValueAsInteger(Const.MONGODB_PORT);
		   mongoClient = new MongoClient( host , port );
		
	}

	public  static synchronized void closeConnection(){
		
    		if(mongoClient != null){
    			
    		    mongoClient.close();
    			
    		}
	}
		
	}

 原创文章,转载请注明出处:

 

http://bigcat2013.iteye.com/blog/2109633

 

谢谢!

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics