2017-11-11 30 views
0

我在這裏查看文檔:https://www.playframework.com/documentation/2.6.x/ScalaDatabase#How-to-configure-several-data-sources。使用在playframework 2.6中配置多個連接池

# 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您可以指定默認配置,數據庫等連接池的原型設置等。我如何定義每個連接池的設置?也許一個連接池只需要一個連接,另一個需要更多連接。

感謝

回答

0

使用數據庫裏面設置的hikaricp設置似乎這樣的伎倆:

db { 
    mentions { 
    driver="org.postgresql.Driver" 
    url="jdbc:postgresql://localhost:5432/mentions" 
    password="***" 
    username="play" 
    hikaricp { 
     maximumPoolSize = 2 
     minimumIdle=2 
    } 

    } 

    postgres { 
    driver="org.postgresql.Driver" 
    url="jdbc:postgresql://localhost:5432/postgres" 
    password="***" 
    username="postgres" 
    hikaricp { 
     maximumPoolSize = 9 
     minimumIdle=9 
    } 
    } 

}