简体   繁体   English

用rails查询外部数据库。 抽象类+建立连接?

[英]Query external database with rails. Abstract Class + establish_connection?

I am trying to establish a connection to an external CRM database (MySQL) in my rails4 app. 我正在尝试在rails4应用程序中建立与外部CRM数据库(MySQL)的连接。 I am able to connect to the DB through console, and i can use the MySQL2 gem to directly connect and query using this approach: 我可以通过控制台连接到数据库,并且可以使用MySQL2 gem通过这种方法直接连接和查询:

connection = Mysql2::Client.new(host:11.12.14.14,username...)
results = connection.query("SELECT * FROM.....")

However, I am confused as to how best to implement and reuse this connection. 但是,对于如何最好地实现和重用此连接,我感到困惑。 I have concerns about connection pooling and opening too many connections... Through researching this topic I found a lot of information about using a model... So I setup an abstract Model, and put the database connection info in my database.yml 我担心连接池和打开太多连接...通过研究此主题,我发现了很多有关使用模型的信息...因此,我建立了一个抽象模型,并将数据库连接信息放入了我的database.yml中。

class ExternalCrm < ActiveRecord::Base
  # No corresponding table in the DB.
  self.abstract_class = true

  establish_connection(:external_crm)

  def self.getCustomerId(first_name, last_name)
    get = connection.select_one("SELECT * FROM customers WHERE First_Name=#{connection.quote(first_name)} AND Last_Name=#{connection.quote(last_name)}")
    get.id
  end

end

This works... and i can run this method BUT why do i have to call the connection ExternalCrm.connection.select_one() rather than just directly saying ExternalCrm.find_by_sql()... also, will calling connection every time use the pool or could it cause a too many connections issue? 这行得通...并且我可以运行此方法,但为什么我必须调用连接ExternalCrm.connection.select_one()而不是直接说ExternalCrm.find_by_sql()...,而且每次使用该池时都会调用连接还是会导致过多的连接问题?

Most of the queries I need to run are just big raw SQL statements, so i don't really need activerecord, and models for each table. 我需要运行的大多数查询只是大的原始SQL语句,因此我实际上并不需要activerecord和每个表的模型。 So I am not sure if using an abstract model is the best approach here... 所以我不确定在这里使用抽象模型是否是最好的方法...

My goal is to establish the connection, and then be able to run methods the connection... for example ExternalCrm.getUserId(firstname,lastname) 我的目标是建立连接,然后能够运行连接的方法...例如, ExternalCrm.getUserId(firstname,lastname)

What would be the best way to accomplish this? 做到这一点的最佳方法是什么?

Reusing connection. 重用连接。

You have already wrapped up in a class, hence a connection will remain open all the time and it's a good approach, and the problem of multiple connections not to be worried. 您已经完成了一个课程,因此连接将一直保持打开状态,这是一个好方法,并且不必担心多个连接的问题。


You haven't specified if you are dealing with multiple table tables with ExternalCrm connection, if not 您尚未指定是否要通过ExternalCrm连接处理多个表表,如果不是

self.table_name = 'customers' 

would be good then access class with ease ExternalCrm for using any methods., like, 然后可以轻松地使用ExternalCrm方法访问类,以使用任何方法。

ExternalCrm.getUserId(firstname,lastname)

In case if tables are changing you could propably reset every connection is completed with the table interaction, with 万一表发生变化,您可以适当地重置每个连接,只要交互完成即可,

reset_column_information reset_column_information

method. 方法。

Check my blog for more. 检查我的博客了解更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails中与多个数据库并行建立连接? - How to establish_connection with more than one database in parallel in Rails? Rails无法使用ActiveRecord :: establish_connection连接到数据库 - Rails cant connect to database with ActiveRecord::establish_connection Rails-建立连接和嵌套表单 - Rails - establish_connection and nested forms 测试和建立连接 - testing and establish_connection rails和多数据库,在运行时使用Establishment_connection路由到正确的数据库? - rails and multi dbs, using establish_connection on the fly to route to the correct database? 无法打开rails console:生成数据库未配置,establish_connection引发ActiveRecord :: AdapterNotSpecified - Can't open rails console: production database not configured, establish_connection raises ActiveRecord::AdapterNotSpecified Rails 6 和 Puma 是否仍需要在工作启动时建立连接? - Is establish_connection on worker boot still required on Rails 6 and Puma? rails 3 Establishment_connection对切换数据库不起作用 - rails 3 establish_connection doesn't work for switching databases 您可以使用Establishment_connection在Rails中连接到SQL视图吗 - Can you use establish_connection to connect to an SQL view in rails 动态子类化和ActiveRecord建立与PostgreSQL数据库的连接 - Dynamic Subclassing and ActiveRecord establish_connection with PostgreSQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM