mongodb java接入

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 21:42 点击:

mongodb的java驱动已经提供比较全的操作,分为三块:

1.普通用户操作

2.管理员操作

3.oplog操作

具体可以添加一个eclipse testng插件,再在工程中导入testng.jar包即可看到。

本文主要说明各个使用点和注意事项。

 


1.连接一个复制集(replica sets)mongod:

复制集是mongo解决稳定性的一个方案,也是可以实现读写分离的一种实现,这里介绍复制集的java连接方式。

    static Mongo m; 
     
    static { 
        List<ServerAddress> addresslist = new ArrayList<ServerAddress>(); 
        try { 
            addresslist.add(new ServerAddress("127.0.0.1:10240")); 
            addresslist.add(new ServerAddress("127.0.0.1:11240")); 
            addresslist.add(new ServerAddress("127.0.0.1:12240")); 
        } catch (UnknownHostException e) { 
            System.err.println("address check error."); 
            System.exit(-1); 
        } 
         
        MongoOptions options = new MongoOptions(); 
        options.autoConnectRetry = true; 
        options.connectionsPerHost = 20; 
        options.connectTimeout = 6000; 
        options.maxAutoConnectRetryTime = 12000; 
        options.maxWaitTime = 12000; 
        options.socketKeepAlive = true; 
        options.socketTimeout = 2000; 
         
        try { 
            m = new Mongo(addresslist, options); 
        } catch (MongoException e) { 
            System.err.println("mongo create error."); 
            System.exit(-1); 
        } 
    } 
    static Mongo m;
   
    static {
        List<ServerAddress> addresslist = new ArrayList<ServerAddress>();
        try {
            addresslist.add(new ServerAddress("127.0.0.1:10240"));
            addresslist.add(new ServerAddress("127.0.0.1:11240"));
            addresslist.add(new ServerAddress("127.0.0.1:12240"));
        } catch (UnknownHostException e) {
            System.err.println("address check error.");
            System.exit(-1);
        }
       
       

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Request.ServerVariables 参数大全
      • 执行全文索引时出现权限不足的解决方法
      • 导入excel文件处理流程节点的解决方案
      • 查看sql修改痕迹(SQL Change Tracking on Table)
      • App数据层设计及云存储使用指南
      • PostgreSQL启动过程中的那些事三:加载GUC参数
      • MongoDB安装为Windows服务方法与注意事项
      • Percolator与分布式事务思考(二)
      • 写给MongoDB开发者的50条建议Tip1
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1