java链接mongo

一、链接方式:

1、单台服务器或主从模式:

点击(此处)折叠或打开

  1. Mongo mongo1 = new Mongo( "127.0.0.1" );
  2. Mongo mongo2 = new Mongo( "127.0.0.1", 27017 );
  3. Mongo mongo3 = new Mongo( new DBAddress( "127.0.0.1", 27017, "test" ) );
  4. Mongo mongo4 = new Mongo( new ServerAddress( "127.0.0.1") );
四种方式均可,默认的链接端口是27017。

2、复制集模式链接:
请注意,单台机器上配置的复制集最好使用真实ip初始化和链接,不建议使用127.0.0.1或者localhost,否则容易链接不上,详见此篇blog


点击(此处)折叠或打开

  1. static Mongo m = null;
  2. static{
  3. try {
  4. List<ServerAddress> list= new ArrayList<ServerAddress>();
  5. ServerAddress sap0 = new ServerAddress("192.168.132.100",20011);
  6. ServerAddress sas1 = new ServerAddress("192.168.132.100",20012);
  7. ServerAddress sas2 = new ServerAddress("192.168.132.100",20013);
  8. list.add(sap0);
  9. list.add(sas1);
  10. list.add(sas2);
  11. m = new Mongo(list);
  12. } catch (UnknownHostException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }

二、连接选项:
mongo-java-driver中提供了一个类MongoOption,用于初始化链接参数,主要有一下这些:
1、常规选项:
①、
connectionsPerHost :单个主机连接到mongo实例允许的最大连接数。这其实相当于c3p0的 maxPoolSize 参数,mongo是内建连接池的,功能跟c3p0等差不多,超过了就会新建链接,默认值是10,大并发的话较小。
②、threadsAllowedToBlockForConnectionMultiplier:这个参数是跟connectionsPerHost配套的,当连接数超过connectionsPerHost的时候,需要建立新的连接,连接请求会被阻塞,这个参数就代表允许阻塞请求的最大值,超过这个值之后的请求都会报错。默认值5。
③、
maxWaitTime :线程等待链接可用的最长时间,ms单位,默认120,000,两分钟。
④、connectTimeout:建立链接的超时时间。默认为0,代表永不超时。
⑤、
socketTimeout :执行io操作的超时时间,默认为0,代表不超时。
⑥、
socketKeepAlive :为防火墙设置的,保证socket存活。默认false。
⑦、
autoConnectRetry :自动重连,连接池都有的参数。但是在mongo里显的比较鸡肋,不管设置false还是true,mongo-java-driver本身就有重连机制,而且当是复制集的情况下,如果主库宕机,他只会重连宕机机器的ip,我不知道这个是怎么处理的,以后看源码吧,默认false,看来本来也不建议打开。
⑧、
maxAutoConnectRetryTime :我去,坑爹啊,竟然是时间!默认为0,代表15s。。。咋想的。
⑨、
slaveOk :用于读写分离,废弃了(mongo2.0/driver2.7),详见Mongo读写分离
使用:
2、特殊选项:
Driver对数据库的写操作分几个安全级别,均是通过
WriteConcern 类控制,在MongoOption中定义了 WriteConcern 中使用的全局参数。
①、
WriteConcern 中的几个安全级别

点击(此处)折叠或打开

  1. /** No exceptions are raised, even for network issues */
  2.     public final static WriteConcern NONE = new WriteConcern(-1);

  3.     /** Exceptions are raised for network issues, but not server errors */
  4.     public final static WriteConcern NORMAL = new WriteConcern(0);

  5.     /** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */
  6.     public final static WriteConcern SAFE = new WriteConcern(1);

  7.     /** Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation */
  8.     public final static WriteConcern MAJORITY = new Majority();

  9.     /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk*/
  10.     public final static WriteConcern FSYNC_SAFE = new WriteConcern(true);

  11.     /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk*/
  12.     public final static WriteConcern JOURNAL_SAFE = new WriteConcern( 1, 0, false, true );

  13.     /** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/
  14.     public final static WriteConcern REPLICAS_SAFE = new WriteConcern(2);
NONE的级别最低,不管出了啥事儿,客户端一股脑的往mongo插入,连网络断了都不管。REPLICAS_SAFE的级别最高,他要等从库都同步完才返回给客户端插入成功,复制集的话要至少两台同步完才行,分布式事务啊,牛叉,但是,它没有事务。。。开玩喜了。
②、参数解释:
WriteConcern的初始化函数,一通重载之后调用:

点击(此处)折叠或打开

  1. public WriteConcern( int w , int wtimeout , boolean fsync , boolean j, boolean continueOnInsertError){
  2.         _w = w;
  3.         _wtimeout = wtimeout;
  4.         _fsync = fsync;
  5.         _j = j;
  6.         _continueOnErrorForInsert = continueOnInsertError;
  7.     }
这个函数指定写操作需要等待的server的数量和抛出异常的行为。
w:代表server的数量:。
w=-1 不等待,不做异常检查
w=0 不等待,只返回网络错误
w=1 检查本机,并检查网络错误
w>1 检查w个server,并返回网络错误
wtimeout
:写操作超时的时间。
fsync
:是不是等待刷新数据到磁盘,参见FSYNC_SAFE的注释。
j :是不是等待提交的数据已经写入到日志,并刷新到磁盘,参见JOURNAL_SAFE的注释。
MongoOption中有全局的设置。还有一个

safe
:相当于w=1,wtimeout=0,fsync和j为false,如果这几个指定了,safe不起作用。参见SAFE的注释

三、MongoOption的使用:

点击(此处)折叠或打开

  1. MongoOptions op = new MongoOptions();
  2. op.safe=true;
  3. op.connctionPerHost=50;
  4. op.connctionTimeout=120000;
  5. ....
  6. //list是serverAddress的列表
  7. Mongo m = new Mongo(list,op);
WriteConcern设置好之后用

点击(此处)折叠或打开

  1. m.setWriteConcern(wc);

调用


博客推荐文章

转载自:http://blog.chinaunix.net/uid-15795819-id-3162162.html

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值