连接到多个数据库elixir

连接到多个数据库elixir,elixir,phoenix-framework,Elixir,Phoenix Framework,在Rails中,我们可以动态连接多个数据库 ActiveRecord::Base.establish_connection( database: "db_name", username: "postgres") 但是在Elixir-Phoenix框架中,如何执行相同的过程。使用Mix.Config 配置:我的应用程序,MyApp.OneRepo, 适配器:exto.Adapters.MySQL, 数据库:“遗留数据库”, 用户名:“用户名”, 密码:“密码”, 主机名:“something

在Rails中,我们可以动态连接多个数据库

ActiveRecord::Base.establish_connection(
  database: "db_name", username: "postgres")
但是在Elixir-Phoenix框架中,如何执行相同的过程。

使用Mix.Config
配置:我的应用程序,MyApp.OneRepo,
适配器:exto.Adapters.MySQL,
数据库:“遗留数据库”,
用户名:“用户名”,
密码:“密码”,
主机名:“something.com”
配置:我的应用程序,MyApp.TwoRepo,
适配器:exto.Adapters.Postgres,
用户名:“用户名”,
密码:“密码”,
数据库:“一些”,
主机名:“example.com”
配置:my_应用程序,异位报告:[MyApp.OneRepo,MyApp.TwoRepo]
lib/my_app.ex 监督回购协议

defmodule数据库做什么
使用应用程序
def启动(_类型,_参数)do
导入主管。规范,警告:false
儿童=[
主管(MyApp.OneRepo,[]),#some_table=MyApp.OneRepo.get_by(SomeTable,%{用户名:“Alpha”})
iex>other_table=MyApp.TwoRepo.all from ot in OtherTable,
其中:ot.user\u id==^user.id

对于原始连接,需要使用低于顶部提供的
exto
的抽象级别。基本上,对于
MySQL
数据库,类似这样的操作应该:

with {:ok, conn} <- Mariaex.start_link(opts) do
  value = Ecto.Adapters.MySQL.Connection.execute(conn, sql, [], opts)
  GenServer.stop(conn)
  value
end

使用{:ok,conn}
defmodule数据库执行
⇐ 模块名应该是一个atom(大写的
Databases
,前面加了冒号
:Databases
),但这并不像OP的Rails示例那样是“动态的”。@Dogbert manual explicit
GenServer.start/3
。@Dogbert我没有检查“动态的”备注。我不知道。有多个数据库(每个客户端一个数据库)。我认为上述方法不起作用。我们正在停止GenServer,但Sql连接不起作用。这会使Sql连接保持打开状态。如何解决此问题。
Mariaex
必须在她的
stop
callback中处理基础连接。GenServer.stop将仅停止GenServer进程,但Sql查询连接保持不变闲置的