标签: multiple-databases

如何使用Fluent NHibernate和多个数据库识别特定实体的会话工厂

问题接着来自Fluent NHibernate +多个数据库(不需要关注此链接,这里应该有足够的背景).

我的问题是:

我正在使用Fluent NHibernate.我的应用使用多个数据库.每个数据库都有自己的实体注册(映射)它.结果是具有多个会话工厂,每个工厂与单个DB相关,并且每个会议工厂"包含"其自己的一组映射实体.

对于加载实体,我创建了一个通用的Factory类,它提供了一些可用于任何注册实体(在任何DB中)的标准加载方法.问题是:加载方法需要为我正忙着处理的实体类使用正确的会话工厂.我如何确定需要使用哪个会话工厂?我手头有所有的Session Factories(并且由数据库名称索引),我只需要一种方法,只知道我要加载的实体的类型,选择正确的Session Factory来使用.

例如:

public IBaseBusinessObject CreatePopulatedInstance(Type boType, Guid instanceKey)
{
  IBaseBusinessObject result = null;
  ISessionFactory sessionFactory = GetSessionFactory(boType);
  using (ISession session = sessionFactory.OpenSession())
  {
    using (session.BeginTransaction())
    {
      result = (IBaseBusinessObject)session.Get(boType, instanceKey);
    }
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

需要在GetSessionFactory(boType)中进行什么?

谢谢阅读!

c# fluent-nhibernate multiple-databases

7
推荐指数
1
解决办法
2998
查看次数

CodeIgniter多个数据库持久连接?

我在我的CodeIgniter应用程序中使用多个数据库,并且已经阅读了很多应该关闭持久连接的数据库.

为什么推荐这个措施,这在最新版本2.0.2中仍然是必要的吗?

我正在做的事情

$db2 = $this->load->database("dbname", TRUE);
Run Code Online (Sandbox Code Playgroud)

php mysql codeigniter multiple-databases persistent-connection

7
推荐指数
1
解决办法
2328
查看次数

Django TestCase不在辅助数据库上使用事务

我正在使用Django 1.3.1.我有两个数据库,我的一些模型存在于一个数据库中,一些存在于另一个数据库中.这两个数据库都是contrib.gis.db.backends.postgis数据库.

令我惊讶的是,Django的TestCase并没有回滚我在测试之间在辅助数据库中所做的更改.

在下面的代码中,myproject.models.WellOwner是一个非常简单的模型,基本上只有一个字段"name".路由器说它应该在辅助数据库中.第一次测试中的断言成功,第二次测试失败:

from django.test import TestCase
from myproject.models import WellOwner

class SimpleTest(TestCase):
    def test1(self):
        WellOwner.objects.create(name="Remco")
        self.assertEquals(1, WellOwner.objects.count())  # Succeeds

class SimpleTest2(TestCase):
    def test2(self):
        # I would expect to have an empty database at this point
        self.assertEquals(0, WellOwner.objects.count())  # Fails!
Run Code Online (Sandbox Code Playgroud)

我假设Django将此包装在默认数据库的事务中,但不包含在辅助数据库上.这是一个已知的问题吗?有修复吗?也许在1.4?我的Google-fu失败了.

(如果我在设置中将DATABASE_ROUTERS更改为[],以便所有内容都进入同一个数据库,问题就会消失)

我将添加路由器的整个代码,以防它有用:

SECONDARY_MODELS = ('WellOwner', ...)

import logging
logger = logging.getLogger(__name__)


class GmdbRouter(object):
    """Keep some models in a secondary database."""

    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'gmdb':
            if model._meta.object_name in SECONDARY_MODELS:
                return 'secondary'

        return None

    def db_for_write(self, …
Run Code Online (Sandbox Code Playgroud)

python django multiple-databases

7
推荐指数
1
解决办法
794
查看次数

Grails - 多个数据源

首先,让我开始,我已经看了很多关于在Grails上配置多个数据源的"正确"方法的网站,其中每一个(使用Grails 2.0及更高版本)指向文档,但是在做了文档之后我说得到的这个错误:

Error 2014-03-29 15:48:29,219 [localhost-startStop-1] ERROR context.GrailsContextLoader      - Error initializing the application: Error creating bean with name 'transactionManager_lookup': Cannot resolve reference to bean 'sessionFactory_lookup' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_lookup': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean '$primaryTransactionManager' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean …
Run Code Online (Sandbox Code Playgroud)

grails multiple-databases oracle11g multi-tenant

7
推荐指数
1
解决办法
1846
查看次数

BigQuery - 合并表

我在 BigQuery 中有每月数据,但我想创建一个年度数据库,这意味着将 12 个子数据库合并为 1 个。

怎么做到呢?

所有 12 个数据库的结构在形式上都是相同的:

日期、名称、金额、价值、Type_of_Good

我认为 JOIN 可能对我有帮助,但事实并非如此。

谢谢

multiple-databases google-bigquery

7
推荐指数
2
解决办法
6069
查看次数

为什么django_migrations表在所有数据库中

我建立一个网站下Django框架,这个网站需要有不同的SQL计划,现在我成功创建的所有计划和所有的东西,但我不明白为什么表django_migrations是在数据库迁移后每个模式.

  • 预期数据库内容:

    AppDB表是此应用程序定义的所有模型

    默认数据库表都是Django表(admin,contenttypes,auth,sessions)

  • 数据库内容:

    AppDB表是此app + django_migrations定义的所有模型

    DEFAULT表都是Django表(admin,contenttypes,auth,sessions)+ django_migrations

这些是2 dbs的路由器:

class DefaultRouter(object):
    APPS = ['auth', 'sessions', 'admin', 'contenttypes']
    DB = 'default'

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.APPS:
            return self.DB
        return None

    def db_for_write(self, model, **hints):

        if model._meta.app_label in self.APPS:
            return self.DB

        return None

    def allow_relation(self, obj1, obj2, **hints):

        if obj1._meta.app_label in self.APPS or obj2._meta.app_label in self.APPS:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):

        if app_label in self.APPS:
            return db == self.DB
        return None …
Run Code Online (Sandbox Code Playgroud)

python django configuration database-migration multiple-databases

7
推荐指数
1
解决办法
3359
查看次数

Spring boot MongoRepository 中的“找不到类型类的 PersistentEntity”异常

在这里,我在 mongodb 中配置了两个数据库。如本教程(链接)中所述。所以基本上我覆盖MongoDataAutoConfigurationMongoProperties实现。

属性 yml 文件:

spring:
  autoconfigure:
       exclude: 
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration

mongodb:
   primary:
     host: 127.0.0.1
     port: 27017
     database: db_admin_crm
   secondary:
     host: 127.0.0.1
     port: 27017
     database: lead_forms
Run Code Online (Sandbox Code Playgroud)

MultipleMongoProperties 类:

@Data
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {

 private MongoProperties primary = new MongoProperties();
 private MongoProperties secondary = new MongoProperties();
    //getters and setters
}
Run Code Online (Sandbox Code Playgroud)

MultipleMongoConfig 类:

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {

@Autowired
private final MultipleMongoProperties mongoProperties;

public MultipleMongoConfig() {
    mongoProperties = null;
}


@Primary
@Bean(name …
Run Code Online (Sandbox Code Playgroud)

multiple-databases spring-data-mongodb mongorepository spring-boot

7
推荐指数
0
解决办法
4570
查看次数

Rails中的多个数据库连接

我正在使用active_delegate在Rails中进行多个连接.这里我使用mysql作为master_database用于某些模型,而postgresql用于其他一些模型.

问题是,当我尝试访问mysql模型时,我收到以下错误!堆栈跟踪显示,它仍然使用postgresql适配器来访问我的mysql模型!

RuntimeError: ERROR C42P01  Mrelation "categories" does not exist   P15 F.\src\backend\parser\parse_relation.c  L886    RparserOpenTable: SELECT * FROM "categories" 

STACKTRACE
===========
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `execute'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:81:in `cache_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:661:in `find_by_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1553:in `find_every'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:615:in `find'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/controllers/categories_controller.rb:48:in `index'
Run Code Online (Sandbox Code Playgroud)

这是我的database.yml档案

postgre: &postgre
  adapter: postgresql
  database: codex
  host: localhost
  username: postgres
  password: root
  port: 5432  

mysql: &mysql
  adapter: mysql
  database: project
  host: localhost
  username: root
  password: root
  port: 3306  

development: …
Run Code Online (Sandbox Code Playgroud)

mysql postgresql ruby-on-rails multiple-databases

6
推荐指数
3
解决办法
2万
查看次数

在codeigniter中加入两个数据库的查询

我需要编写来自两个数据库的两个表的连接查询并获取连接的数据.例如,考虑我有一个数据库db1,它有一些名为companies,plans,customers的表.假设我需要加入两个表公司并计划在另一个数据库db2上使用另一个表'cdr',通过使用类似的列对它们进行分组.

我现在正在运行的查询如下:

function get_per_company_total_use ($custid)
        {         
                 $this->DB1->select('ph_Companies.CompanyName');
                 $this->DB1->where('ph_Companies.Cust_ID', $custid);
                 $this->DB2->select_sum('cdr.call_length_billable')->from('cdr');
                 $this->DB2->group_by('cdr.CompanyName');
                 $this->db->join('Kalix2.ph_Companies', 'Kalix2.ph_Companies.CompanyName = Asterisk.cdr.CompanyName');
                 $query = $this->db->get();
                 if($query->result()){
                     foreach ($query->result() as $value) {
                         $companies[]= array($value->CompanyName,$value->call_length_billable);
                          }
                     return $companies;
                 }
                 else 
                     return FALSE;
        }
Run Code Online (Sandbox Code Playgroud)

但我的查询不是获取数据并抛出错误.同样的查询,我在一个数据库上运行并正在运行.但我需要帮助才能找到如何使用两个数据库完成此操作.

php mysql join multiple-databases codeigniter-2

6
推荐指数
1
解决办法
1万
查看次数

没有将记录保存到 Django 中的 mongodb 数据库中,实际上我正在尝试在单个应用程序中探索多个不同的数据库(Postgresql、Mongodb)

我正在尝试在单个应用程序(待办事项)中使用多个不同的数据库。我正在使用 Djongo 包来处理 mongodb。

设置.py

DATABASES = {
    'default':{},
    'sql_db': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'my_db',
        'USER': '******',
        'PASSWORD': '***',
        'HOST': 'localhost',
        'PORT': '5432',
    },
    'mongodb':{
        'ENGINE': 'djongo',
        'NAME': 'mongo_db'
    }
}
Run Code Online (Sandbox Code Playgroud)

待办事项/模型.py

class Task(models.Model):
    todo = models.CharField(max_length=200)
    status = models.BooleanField(default=False)
    
    def __str__(self):
        return self.todo
Run Code Online (Sandbox Code Playgroud)

todo/serializers.py

class TodoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = '__all__'
Run Code Online (Sandbox Code Playgroud)

待办事项/视图.py

@api_view(['POST'])
def todoCreate(request):
    serializer = TodoSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        serializer.save(using='mongodb')
    return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)

它成功地将记录保存到“sql_db”中,但没有保存在“mongodb”中。

database django orm multiple-databases djongo

6
推荐指数
1
解决办法
45
查看次数