我在这里查看了文档:https ://www.playframework.com/documentation/2.6.x/ScalaDatabase#How-to-configure-several-data-sources 。您可以定义多个数据库连接:
# Orders database
db.orders.driver=org.h2.Driver
db.orders.url="jdbc:h2:mem:orders"
# Customers database
db.customers.driver=org.h2.Driver
db.customers.url="jdbc:h2:mem:customers"
您可以使用以下代码在代码中引用:
package controllers
import javax.inject.Inject
import play.api.mvc.{BaseController, ControllerComponents}
import play.api.db.{Database, NamedDatabase}
// inject "orders" database instead of "default"
class ScalaInjectNamed @Inject()(
@NamedDatabase("orders") db: Database,
val controllerComponents: ControllerComponents
) extends BaseController {
// do whatever you need with the db
}
但是如何为每个连接自定义连接池呢?
根据此文档:https ://www.playframework.com/documentation/2.6.x/SettingsJDBC您可以指定默认配置、数据库等连接池的原型设置等。如何定义每个连接池的设置?也许一个连接池只需要一个连接,而另一个需要更多。
谢谢