简体   繁体   中英

Connection pooling in slick?

是否有一种简单的方法可以将数据库连接池与scala的Slick一起使用

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).

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

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

Play 2.4现在使用的HikariCP非常好看: https //brettwooldridge.github.io/HikariCP/ https://www.playframework.com/documentation/2.4.x/SettingsJDBC

似乎更高版本的播放池配置了连接 - 请参阅http://www.playframework.com/documentation/2.0.1/SettingsJDBC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM