我有一个 Scala/Play 应用程序,我需要使用 Slick 2 在同一台服务器上连接来自 2 个不同 PostgreSQL 数据库的表。数据库很大,所以我不手动为我的表创建类,我使用Slick 2 模式代码生成器,因此,如果需要进行更改,则必须使用代码生成器。
而不是像这样在conf中定义我的db url
db.pg1.url="jdbc:postgresql://localhost:5432/db1"
db.pg2.url="jdbc:postgresql://localhost:5432/db2"
...
我想这样做:
db.pg.url="jdbc:postgresql://localhost:5432"
然后我的方法看起来像这样:
def test = DB("pg").withDynSession { //not sure if this is 100% correct
//query joining two tables from different databases here
}
如,连接到服务器,而不是特定的数据库,这样我就可以使用相同的连接处理多个数据库。这可能吗?我已经在 Slick Plain SQL 中使用了这种方法,并且效果很好。附带问题:这会对性能产生一些负面影响吗?
如果我想完成的事情是可能的,请你给我一个简单的例子。如果这是不可能的,我有什么选择?谢谢。
我的堆栈:
- 斯卡拉 2.10.3
- 玩2.2.2
- 光滑 2.0
- PostgreSQL 9.3.3
- 爪哇 8