[英]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 万一表发生变化,您可以适当地重置每个连接,只要表交互完成即可,
method. 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.