找到你要的答案

Q:Connection pooling in slick?

Q:连接池在光滑?

Is there an easy way to use a DB connection pool with scala's Slick?

有使用数据库连接池与Scala的斯利克一个容易的方法吗?

answer1: 回答1:

I use Apache Commons DBCP for this. Basically, you simply create a DataSource, that encapsulates the pooling details, and pass that DataSource to Slick:

import org.apache.commons.dbcp.BasicDataSource

val dataSource: DataSource = {
  val ds = new BasicDataSource
  ds.setDriverClassName("org.hsqldb.jdbc.JDBCDriver")
  ds.setUsername("SA")
  ds.setPassword("")
  ds.setMaxActive(20);
  ds.setMaxIdle(10);
  ds.setInitialSize(10);
  ds.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS")
  new java.io.File("target").mkdirs // ensure that folder for database exists
  ds.setUrl("jdbc:hsqldb:file:target/db/db")
  ds
}

// test the data source validity
dataSource.getConnection().close()

// get the Slick database that uses the pooled connection
val database = Database.forDataSource(dataSource)

This example uses HSQLDB, but can be easily adapted to any other database.

Full example is here (you can clone the project, and run sbt run in lift/ directory to see it working).

我使用Apache Commons DBCP这。基本上,您只需创建一个数据源,浓缩池的详细信息,并通过数据源斯利克:

import org.apache.commons.dbcp.BasicDataSource

val dataSource: DataSource = {
  val ds = new BasicDataSource
  ds.setDriverClassName("org.hsqldb.jdbc.JDBCDriver")
  ds.setUsername("SA")
  ds.setPassword("")
  ds.setMaxActive(20);
  ds.setMaxIdle(10);
  ds.setInitialSize(10);
  ds.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS")
  new java.io.File("target").mkdirs // ensure that folder for database exists
  ds.setUrl("jdbc:hsqldb:file:target/db/db")
  ds
}

// test the data source validity
dataSource.getConnection().close()

// get the Slick database that uses the pooled connection
val database = Database.forDataSource(dataSource)

这个例子使用HSQLDB,但可以很容易地适应其他数据库。

完整的例子在这里(你可以复制的项目,并运行在电梯/目录运行SBT看工作)。

answer2: 回答2:

For completion I ended up writing a blog post about this:

http://fernandezpablo85.github.io/2013/04/07/slick_connection_pooling.html

为了完成我最后写了一篇博客文章:

http://fernandezpablo85.github.io/2013/04/07/slick_connection_pooling.html

answer3: 回答3:

Play 2.4 now uses HikariCP which looks really nice: https://brettwooldridge.github.io/HikariCP/ https://www.playframework.com/documentation/2.4.x/SettingsJDBC

Play 2.4 now uses HikariCP which looks really nice: https://brettwooldridge.github.io/HikariCP/ https://www.playframework.com/documentation/2.4.x/SettingsJDBC

answer4: 回答4:

It appears as though the later version of play pool configured connections - see http://www.playframework.com/documentation/2.0.1/SettingsJDBC

看来以后的版本玩池配置连接看http://www.playframework.com/documentation/2.0.1/settingsjdbc

scala  slick