MongoDB是介于关系数据库和非关系数据库之间的一种产品,文件的存储格式为BSON(一种JSON的扩展),这里就主要介绍Java通过使用mongo-2.7.3.jar包实现mongodb连接池,具体的java代码实现如下:
数据库连接池配置参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/**
*@Description: mongo连接池配置文件
*/
package cn.lulei.mongo.pool;
public class MongoConfig {
private static String userName; //用户名
private static String pwd; //密码
private static String[] host; //主机地址
private static int [] port; //端口地址
private static String dbName; //数据库名
private static int connectionsPerHost = 20 ; //每台主机最大连接数
private static int threadsAllowedToBlockForConnectionMultiplier = 10 ; //线程队列数
private static boolean authentication = false ; //是否需要身份验证
public static String getUserName() {
return userName;
}
public static void setUserName(String userName) {
MongoConfig.userName = userName;
}
public static String getPwd() {
return pwd;
}
public static void setPwd(String pwd) {
MongoConfig.pwd = pwd;
}
public static String[] getHost() {
return host;
}
public static void setHost(String[] host) {
MongoConfig.host = host;
}
public static int [] getPort() {
return port;
}
public static void setPort( int [] port) {
MongoConfig.port = port;
}
public static String getDbName() {
return dbName;
}
public static void setDbName(String dbName) {
MongoConfig.dbName = dbName;
}
public static int getConnectionsPerHost() {
return connectionsPerHost;
}
public static void setConnectionsPerHost( int connectionsPerHost) {
MongoConfig.connectionsPerHost = connectionsPerHost;
}
public static int getThreadsAllowedToBlockForConnectionMultiplier() {
return threadsAllowedToBlockForConnectionMultiplier;
}
public static void setThreadsAllowedToBlockForConnectionMultiplier(
int threadsAllowedToBlockForConnectionMultiplier) {
MongoConfig.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
}
public static boolean isAuthentication() {
return authentication;
}
public static void setAuthentication( boolean authentication) {
MongoConfig.authentication = authentication;
}
}
|
数据库连接池管理类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/**
*@Description: mongo数据库连接池管理类
*/
package cn.lulei.mongo.pool;
import java.util.ArrayList;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
public class MongoManager {
private static Mongo mongo;
private DB db;
static {
init();
}
/**
* @param dbName
* @param userName
* @param pwd
* 实例化dbName一个DB
*/
public MongoManager(String dbName, String userName, String pwd) {
if (dbName == null || "" .equals(dbName)) {
throw new NumberFormatException( "dbName is null" );
}
db = mongo.getDB(dbName);
if (MongoConfig.isAuthentication() && !db.isAuthenticated()){
if (userName == null || "" .equals(userName)) {
throw new NumberFormatException( "userName is null" );
}
if (pwd == null || "" .equals(pwd)) {
throw new NumberFormatException( "pwd is null" );
}
db.authenticate(userName, pwd.toCharArray());
}
}
/**
* 使用配置参数实例化
*/
public MongoManager() {
this (MongoConfig.getDbName(), MongoConfig.getUserName(), MongoConfig.getPwd());
}
/**
* @param tableName
* @return
* @Description: 获取表tableName的链接DBCollection
*/
public DBCollection getDBCollection(String tableName) {
return db.getCollection(tableName);
}
/**
* @Description: mongo连接池初始化
*/
private static void init() {
if (MongoConfig.getHost() == null || MongoConfig.getHost().length == 0 ) {
throw new NumberFormatException( "host is null" );
}
if (MongoConfig.getPort() == null || MongoConfig.getPort().length == 0 ) {
throw new NumberFormatException( "port is null" );
}
if (MongoConfig.getHost().length != MongoConfig.getPort().length) {
throw new NumberFormatException( "host's length is not equals port's length" );
}
try {
//服务列表
List<ServerAddress> replicaSetSeeds = new ArrayList<ServerAddress>();
for ( int i = 0 ; i < MongoConfig.getHost().length; i++) {
replicaSetSeeds.add( new ServerAddress(MongoConfig.getHost()[i], MongoConfig.getPort()[i]));
}
//连接池参数设置
MongoOptions options = new MongoOptions();
options.connectionsPerHost = MongoConfig.getConnectionsPerHost();
options.threadsAllowedToBlockForConnectionMultiplier = MongoConfig.getThreadsAllowedToBlockForConnectionMultiplier();
mongo = new Mongo(replicaSetSeeds, options);
//从服务器可读
mongo.setReadPreference(ReadPreference.SECONDARY);
} catch (Exception e){
e.printStackTrace();
}
}
}
|
下面通过一个简单的测试类,来看下如何使用这个连接池~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/**
*@Description:mongo测试
*/
package cn.lulei.mongo.test;
import cn.lulei.mongo.pool.MongoConfig;
import cn.lulei.mongo.pool.MongoManager;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] host = { "127.0.0.1" };
int [] port = { 27001 };
MongoConfig.setHost(host);
MongoConfig.setPort(port);
MongoConfig.setDbName( "novel" );
MongoManager mongoManager = new MongoManager();
mongoManager.getDBCollection( "chapter" );
}
}
|
在使用上述管理类时,只需要初始化MongoConfig类即可。对类MongoManager的实例话,既可以使用MongoConfig的配置也可以自己通过参数来设置,每次获取DBCollection 时,只需要调用getDBCollection(String tableName)方法即可。
以上就是本文的全部内容,希望本文所述对大家学习java程序设计有所帮助。