1. <small id='nmeEq'></small><noframes id='nmeEq'>

  2. <legend id='nmeEq'><style id='nmeEq'><dir id='nmeEq'><q id='nmeEq'></q></dir></style></legend>
  3. <tfoot id='nmeEq'></tfoot>

    <i id='nmeEq'><tr id='nmeEq'><dt id='nmeEq'><q id='nmeEq'><span id='nmeEq'><b id='nmeEq'><form id='nmeEq'><ins id='nmeEq'></ins><ul id='nmeEq'></ul><sub id='nmeEq'></sub></form><legend id='nmeEq'></legend><bdo id='nmeEq'><pre id='nmeEq'><center id='nmeEq'></center></pre></bdo></b><th id='nmeEq'></th></span></q></dt></tr></i><div id='nmeEq'><tfoot id='nmeEq'></tfoot><dl id='nmeEq'><fieldset id='nmeEq'></fieldset></dl></div>

      <bdo id='nmeEq'></bdo><ul id='nmeEq'></ul>
    1. spring-data-mongodb 在一个 Mongo 实例中连接多个数据库

      时间:2024-08-24
      • <small id='8r93Y'></small><noframes id='8r93Y'>

            <tbody id='8r93Y'></tbody>
          <legend id='8r93Y'><style id='8r93Y'><dir id='8r93Y'><q id='8r93Y'></q></dir></style></legend>

          <tfoot id='8r93Y'></tfoot>

          <i id='8r93Y'><tr id='8r93Y'><dt id='8r93Y'><q id='8r93Y'><span id='8r93Y'><b id='8r93Y'><form id='8r93Y'><ins id='8r93Y'></ins><ul id='8r93Y'></ul><sub id='8r93Y'></sub></form><legend id='8r93Y'></legend><bdo id='8r93Y'><pre id='8r93Y'><center id='8r93Y'></center></pre></bdo></b><th id='8r93Y'></th></span></q></dt></tr></i><div id='8r93Y'><tfoot id='8r93Y'></tfoot><dl id='8r93Y'><fieldset id='8r93Y'></fieldset></dl></div>
              • <bdo id='8r93Y'></bdo><ul id='8r93Y'></ul>

                本文介绍了spring-data-mongodb 在一个 Mongo 实例中连接多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用最新的 spring-data-mongodb (1.1.0.M2) 和最新的 Mongo Driver (2.9.0-RC1).我有一种情况,我有多个客户端连接到我的应用程序,我想在同一个 Mongo 服务器中为每个客户端提供自己的模式/数据库".如果我直接使用驱动程序,这不是一项很难实现的任务:

                I am using the latest spring-data-mongodb (1.1.0.M2) and the latest Mongo Driver (2.9.0-RC1). I have a situation where I have multiple clients connecting to my application and I want to give each one their own "schema/database" in the same Mongo server. This is not a very difficult task to achieve if I was using the driver directly:

                Mongo mongo = new Mongo( new DBAddress( "localhost", 127017 ) );
                
                DB client1DB = mongo.getDB( "client1" );
                DBCollection client1TTestCollection = client1DB.getCollection( "test" );
                long client1TestCollectionCount = client1TTestCollection.count();
                
                DB client2DB = mongo.getDB( "client2" );
                DBCollection client2TTestCollection = client2DB.getCollection( "test" );
                long client2TestCollectionCount = client2TTestCollection.count();
                

                看,很简单.但是 spring-data-mongodb 不允许使用多个数据库的简单方法.建立与 Mongo 的连接的首选方法是扩展 AbstractMongoConfiguration 类:

                See, easy. But spring-data-mongodb does not allow an easy way to use multiple databases. The preferred way of setting up a connection to Mongo is to extend the AbstractMongoConfiguration class:

                你会看到你重写了下面的方法:

                You will see that you override the following method:

                getDatabaseName()
                

                所以它迫使您使用一个数据库名称.然后,您构建的存储库接口使用 MongoTemplate 中的数据库名称,该名称传递到 SimpleMongoRepository 类.

                So it forces you to use one database name. The repository interfaces that you then build use that database name inside the MongoTemplate that is passed into the SimpleMongoRepository class.

                究竟我会在哪里粘贴多个数据库名称?我必须创建多个数据库名称、多个 MongoTempate(每个数据库名称一个)和多个其他配置类.这仍然没有让我的存储库接口使用正确的模板.如果有人尝试过这样的事情,请告诉我.如果我弄清楚了,我会在这里发布答案.

                Where on earth would I stick multiple database names? I have to make multiple database names, multiple MongoTempates (one per database name), and multiple other config classes. And that still doesn't get my repository interfaces to use the correct template. If anyone has tried such a thing let me know. If I figure it out I will post the answer here.

                谢谢.

                推荐答案

                所以经过大量的研究和实验,我得出的结论是,目前的 spring-data-mongodb 项目还不可能.我在上面尝试了 baja 的方法并遇到了一个特定的障碍.MongoTemplate 在其构造函数中运行其 ensureIndexes() 方法.此方法调用数据库以确保数据库中存在带注释的索引.MongoTemplate 的构造函数在 Spring 启动时被调用,所以我什至没有机会设置 ThreadLocal 变量.我必须在 Spring 启动时设置默认值,然后在请求进来时更改它.这是不允许的,因为我不想要也没有默认数据库.

                So after much research and experimentation, I have concluded that this is not yet possibly with the current spring-data-mongodb project. I tried baja's method above and ran into a specific hurdle. The MongoTemplate runs its ensureIndexes() method from within its constructor. This method calls out the the database to make sure annotated indexes exist in the database. The constructor for MongoTemplate gets called when Spring starts up so I never even have a chance to set a ThreadLocal variable. I have to have a default already set when Spring starts, then change it when a request comes in. This is not allowable because I don't want nor do I have a default database.

                尽管如此,一切都没有丢失.我们最初的计划是让每个客户端在自己的应用服务器上运行,指向 MongoDB 服务器上自己的 MongoDB 数据库.然后我们可以提供一个 -Dprovider= 系统变量,每个服务器运行时只指向一个数据库.

                All was not lost though. Our original plan was to have each client running on its own application server, pointed at its own MongoDB database on the MongoDB server. Then we can provide a -Dprovider= system variable and each server runs pointing only to one database.

                我们被指示拥有一个多租户应用程序,因此尝试使用 ThreadLocal 变量.但由于它不起作用,我们能够按照我们最初设计的方式运行应用程序.

                We were instructed to have a multi-tenant application, hence the attempt at the ThreadLocal variable. But since it did not work, we were able to run the application the way we had originally designed.

                我相信有一种方法可以使这一切正常工作,它只需要比其他帖子中描述的更多.您必须制作自己的 RepositoryFactoryBean.这是 Spring Data MongoDB 参考文档.您仍然需要实现自己的 MongoTemplate 并延迟或删除 ensureIndexes() 调用.但是您必须重写一些类以确保调用您的 MongoTemplate 而不是 Spring 的.换句话说,工作量很大.我想看到或什至做的工作,我只是没有时间.

                I believe there is a way though to make this all work, it just takes more than is described in the other posts. You have to make your own RepositoryFactoryBean. Here is the example from the Spring Data MongoDB Reference Docs. You would still have to implement your own MongoTemplate and delay or remove the ensureIndexes() call. But you would have to rewrite a few classes to make sure your MongoTemplate is called instead of Spring's. In other words, a lot of work. Work that I would like to see happen or even do, I just did not have the time.

                感谢您的回复.

                这篇关于spring-data-mongodb 在一个 Mongo 实例中连接多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Spring MVC 3:以 JSON 形式返回 Spring-Data 页面 下一篇:如何禁用 Spring Data REST 存储库的默认公开?

                相关文章

                <legend id='rcMjw'><style id='rcMjw'><dir id='rcMjw'><q id='rcMjw'></q></dir></style></legend>

                  <i id='rcMjw'><tr id='rcMjw'><dt id='rcMjw'><q id='rcMjw'><span id='rcMjw'><b id='rcMjw'><form id='rcMjw'><ins id='rcMjw'></ins><ul id='rcMjw'></ul><sub id='rcMjw'></sub></form><legend id='rcMjw'></legend><bdo id='rcMjw'><pre id='rcMjw'><center id='rcMjw'></center></pre></bdo></b><th id='rcMjw'></th></span></q></dt></tr></i><div id='rcMjw'><tfoot id='rcMjw'></tfoot><dl id='rcMjw'><fieldset id='rcMjw'></fieldset></dl></div>
                1. <tfoot id='rcMjw'></tfoot>

                  <small id='rcMjw'></small><noframes id='rcMjw'>

                    <bdo id='rcMjw'></bdo><ul id='rcMjw'></ul>