Permalink
Browse files

Move HikariCPJdbcDataSource into a separate module

  • Loading branch information...
szeiger committed Aug 12, 2015
1 parent b651a62 commit c197fdb3baee85d7ed313ac65b582a2a6a03a1e5
View
@@ -26,16 +26,14 @@ object SlickBuild extends Build {
val testngExtras = Seq(
"com.google.inject" % "guice" % "2.0"
)
val slf4j = "org.slf4j" % "slf4j-api" % "1.6.4"
val slf4j = "org.slf4j" % "slf4j-api" % "1.7.10"
val logback = "ch.qos.logback" % "logback-classic" % "1.1.3"
val typesafeConfig = "com.typesafe" % "config" % "1.2.1"
val reactiveStreamsVersion = "1.0.0"
val reactiveStreams = "org.reactivestreams" % "reactive-streams" % reactiveStreamsVersion
val reactiveStreamsTCK = "org.reactivestreams" % "reactive-streams-tck" % reactiveStreamsVersion
val pools = Seq(
"com.zaxxer" % "HikariCP-java6" % "2.3.7"
)
val mainDependencies = Seq(slf4j, typesafeConfig, reactiveStreams) ++ pools.map(_ % "optional")
val hikariCP = "com.zaxxer" % "HikariCP-java6" % "2.3.7"
val mainDependencies = Seq(slf4j, typesafeConfig, reactiveStreams)
val h2 = "com.h2database" % "h2" % "1.4.187"
val testDBs = Seq(
h2,
@@ -111,7 +109,6 @@ object SlickBuild extends Build {
"-diagrams", // requires graphviz
"-groups"
)),
libraryDependencies ++= Dependencies.mainDependencies,
logBuffered := false,
repoKind <<= (version)(v => if(v.trim.endsWith("SNAPSHOT")) "snapshots" else "releases"),
publishTo <<= (repoKind){
@@ -170,6 +167,7 @@ object SlickBuild extends Build {
test in (reactiveStreamsTestProject, Test),
packageDoc in Compile in slickProject,
packageDoc in Compile in slickCodegenProject,
packageDoc in Compile in slickHikariCPProject,
packageDoc in Compile in slickTestkitProject,
sdlc in aRootProject
)))
@@ -182,13 +180,14 @@ object SlickBuild extends Build {
test := (), testOnly := (), // suppress test status output
commands += testAll,
sdlc := (),
sdlc <<= sdlc dependsOn (sdlc in slickProject, sdlc in slickCodegenProject)
)).aggregate(slickProject, slickCodegenProject, slickTestkitProject)
sdlc <<= sdlc dependsOn (sdlc in slickProject, sdlc in slickCodegenProject, sdlc in slickHikariCPProject)
)).aggregate(slickProject, slickCodegenProject, slickHikariCPProject, slickTestkitProject)
lazy val slickProject: Project = Project(id = "slick", base = file("slick"),
settings = Defaults.coreDefaultSettings ++ sdlcSettings ++ inConfig(config("macro"))(Defaults.configSettings) ++ sharedSettings ++ fmppSettings ++ site.settings ++ site.sphinxSupport() ++ mimaDefaultSettings ++ extTarget("slick") ++ commonSdlcSettings ++ osgiSettings ++ Seq(
name := "Slick",
description := "Scala Language-Integrated Connection Kit",
libraryDependencies ++= Dependencies.mainDependencies,
scalacOptions in (Compile, doc) <++= version.map(v => Seq(
"-doc-source-url", "https://github.com/slick/slick/blob/"+v+"/src/main€{FILE_PATH}.scala",
"-doc-root-content", "scaladoc-root.txt"
@@ -200,6 +199,7 @@ object SlickBuild extends Build {
makeSite <<= makeSite dependsOn (buildCapabilitiesTable in slickTestkitProject),
site.addMappingsToSiteDir(mappings in packageDoc in Compile in slickProject, "api"),
site.addMappingsToSiteDir(mappings in packageDoc in Compile in slickCodegenProject, "codegen-api"),
site.addMappingsToSiteDir(mappings in packageDoc in Compile in slickHikariCPProject, "hikaricp-api"),
site.addMappingsToSiteDir(mappings in packageDoc in Compile in slickTestkitProject, "testkit-api"),
test := (), testOnly := (), // suppress test status output
previousArtifact := Some("com.typesafe.slick" % ("slick_" + scalaBinaryVersion.value) % binaryCompatSlickVersion),
@@ -272,9 +272,8 @@ object SlickBuild extends Build {
))
).configs(DocTest).settings(inConfig(DocTest)(Defaults.testSettings): _*).settings(
unmanagedSourceDirectories in DocTest += (baseDirectory in slickProject).value / "src/sphinx/code",
unmanagedResourceDirectories in DocTest += (baseDirectory in slickProject).value / "src/sphinx/resources",
libraryDependencies ++= Dependencies.pools.map(_ % "test")
) dependsOn(slickProject, slickCodegenProject % "compile->compile")
unmanagedResourceDirectories in DocTest += (baseDirectory in slickProject).value / "src/sphinx/resources"
) dependsOn(slickProject, slickCodegenProject % "compile->compile", slickHikariCPProject % "test->compile")
lazy val slickCodegenProject = Project(id = "codegen", base = file("slick-codegen"),
settings = Defaults.coreDefaultSettings ++ sdlcSettings ++ sharedSettings ++ extTarget("codegen") ++ commonSdlcSettings ++ Seq(
@@ -288,6 +287,25 @@ object SlickBuild extends Build {
)
) dependsOn(slickProject)
lazy val slickHikariCPProject = Project(id = "hikaricp", base = file("slick-hikaricp"),
settings = Defaults.coreDefaultSettings ++ sdlcSettings ++ sharedSettings ++ extTarget("hikaricp") ++ commonSdlcSettings ++ osgiSettings ++ Seq(
name := "Slick-HikariCP",
description := "HikariCP integration for Slick (Scala Language-Integrated Connection Kit)",
scalacOptions in (Compile, doc) <++= version.map(v => Seq(
"-doc-source-url", "https://github.com/slick/slick/blob/"+v+"/slick-hikaricp/src/main€{FILE_PATH}.scala"
)),
libraryDependencies += Dependencies.hikariCP,
test := (), testOnly := (), // suppress test status output
OsgiKeys.exportPackage := Seq("slick.jdbc.hikaricp"),
OsgiKeys.importPackage := Seq(
osgiImport("slick*", slickVersion),
osgiImport("scala*", scalaVersion.value),
"*"
),
OsgiKeys.privatePackage := Nil
)
) dependsOn(slickProject)
lazy val reactiveStreamsTestProject = Project(id = "reactive-streams-tests", base = file("reactive-streams-tests"),
settings = Defaults.coreDefaultSettings ++ sharedSettings ++ testNGSettings ++ Seq(
name := "Slick-ReactiveStreamsTests",
@@ -0,0 +1,58 @@
package slick.jdbc.hikaricp
import java.sql.{Driver, Connection}
import com.typesafe.config.Config
import slick.SlickException
import slick.jdbc.{JdbcDataSourceFactory, JdbcDataSource}
import slick.util.ConfigExtensionMethods._
/** A JdbcDataSource for a HikariCP connection pool.
* See `slick.jdbc.JdbcBackend#Database.forConfig` for documentation on the config parameters. */
class HikariCPJdbcDataSource(val ds: com.zaxxer.hikari.HikariDataSource, val hconf: com.zaxxer.hikari.HikariConfig) extends JdbcDataSource {
def createConnection(): Connection = ds.getConnection()
def close(): Unit = ds.close()
}
object HikariCPJdbcDataSource extends JdbcDataSourceFactory {
import com.zaxxer.hikari._
def forConfig(c: Config, driver: Driver, name: String, classLoader: ClassLoader): HikariCPJdbcDataSource = {
if(driver ne null)
throw new SlickException("An explicit Driver object is not supported by HikariCPJdbcDataSource")
val hconf = new HikariConfig()
// Connection settings
hconf.setDataSourceClassName(c.getStringOr("dataSourceClass", null))
Option(c.getStringOr("driverClassName", c.getStringOr("driver"))).map(hconf.setDriverClassName _)
hconf.setJdbcUrl(c.getStringOr("url", null))
c.getStringOpt("user").foreach(hconf.setUsername)
c.getStringOpt("password").foreach(hconf.setPassword)
c.getPropertiesOpt("properties").foreach(hconf.setDataSourceProperties)
// Pool configuration
hconf.setConnectionTimeout(c.getMillisecondsOr("connectionTimeout", 1000))
hconf.setValidationTimeout(c.getMillisecondsOr("validationTimeout", 1000))
hconf.setIdleTimeout(c.getMillisecondsOr("idleTimeout", 600000))
hconf.setMaxLifetime(c.getMillisecondsOr("maxLifetime", 1800000))
hconf.setLeakDetectionThreshold(c.getMillisecondsOr("leakDetectionThreshold", 0))
hconf.setInitializationFailFast(c.getBooleanOr("initializationFailFast", false))
c.getStringOpt("connectionTestQuery").foreach { s =>
hconf.setJdbc4ConnectionTest(false)
hconf.setConnectionTestQuery(s)
}
c.getStringOpt("connectionInitSql").foreach(hconf.setConnectionInitSql)
val numThreads = c.getIntOr("numThreads", 20)
hconf.setMaximumPoolSize(c.getIntOr("maxConnections", numThreads * 5))
hconf.setMinimumIdle(c.getIntOr("minConnections", numThreads))
hconf.setPoolName(name)
hconf.setRegisterMbeans(c.getBooleanOr("registerMbeans", false))
// Equivalent of ConnectionPreparer
hconf.setReadOnly(c.getBooleanOr("readOnly", false))
c.getStringOpt("isolation").map("TRANSACTION_" + _).foreach(hconf.setTransactionIsolation)
hconf.setCatalog(c.getStringOr("catalog", null))
val ds = new HikariDataSource(hconf)
new HikariCPJdbcDataSource(ds, hconf)
}
}
@@ -119,9 +119,10 @@ trait JdbcBackend extends RelationalBackend {
/** Load a database configuration through [[https://github.com/typesafehub/config Typesafe Config]].
*
* The main config key to set is `connectionPool`. It determines the connection pool
* implementation to use. The default is `HikariCP` (a.k.a. `slick.jdbc.HikariCPJdbcDataSource`)
* for [[https://github.com/brettwooldridge/HikariCP HikariCP]]). Use `disabled` (a.k.a.
* `slick.jdbc.DataSourceJdbcDataSource`) to disable connection pooling and use a DataSource or
* implementation to use. The default is `HikariCP` (a.k.a. `slick.jdbc.hikaricp.HikariCPJdbcDataSource$`)
* for [[https://github.com/brettwooldridge/HikariCP HikariCP]]). This requires the "slick-hikaricp"
* dependency to be added to your project, in addition to "slick" itself. Use `disabled` (a.k.a.
* `slick.jdbc.DataSourceJdbcDataSource$`) to disable connection pooling and use a DataSource or
* the DriverManager directly. A third-party connection pool implementation can be selected by
* specifying the fully qualified name of an object implementing [[JdbcDataSourceFactory]].
*
@@ -6,8 +6,7 @@ import java.util.concurrent.TimeUnit
import java.sql.{SQLException, DriverManager, Driver, Connection}
import javax.sql.DataSource
import com.typesafe.config.Config
import slick.util.ClassLoaderUtil
import slick.util.BeanConfigurator
import slick.util.{Logging, ClassLoaderUtil, BeanConfigurator}
import slick.util.ConfigExtensionMethods._
import slick.SlickException
@@ -23,16 +22,21 @@ trait JdbcDataSource extends Closeable {
def close(): Unit
}
object JdbcDataSource {
object JdbcDataSource extends Logging {
/** Create a JdbcDataSource from a `Config`. See [[JdbcBackend.DatabaseFactoryDef.forConfig]]
* for documentation of the supported configuration parameters. */
def forConfig(c: Config, driver: Driver, name: String, classLoader: ClassLoader): JdbcDataSource = {
def loadFactory(name: String): JdbcDataSourceFactory = {
val clazz = classLoader.loadClass(name)
clazz.getField("MODULE$").get(clazz).asInstanceOf[JdbcDataSourceFactory]
}
val pf: JdbcDataSourceFactory = c.getStringOr("connectionPool", "HikariCP") match {
case "disabled" => DataSourceJdbcDataSource
case "HikariCP" => HikariCPJdbcDataSource
case name =>
val clazz = classLoader.loadClass(name)
clazz.getField("MODULE$").get(clazz).asInstanceOf[JdbcDataSourceFactory]
case "HikariCP" => loadFactory("slick.jdbc.hikaricp.HikariCPJdbcDataSource$")
case "slick.jdbc.HikariCPJdbcDataSource" =>
logger.warn("connectionPool class 'slick.jdbc.HikariCPJdbcDataSource$' has been renamed to 'slick.jdbc.hikaricp.HikariCPJdbcDataSource$'")
loadFactory("slick.jdbc.hikaricp.HikariCPJdbcDataSource$")
case name => loadFactory(name)
}
pf.forConfig(c, driver, name, classLoader)
}
@@ -171,56 +175,6 @@ object DriverJdbcDataSource extends JdbcDataSourceFactory {
}
}
/** A JdbcDataSource for a HikariCP connection pool */
class HikariCPJdbcDataSource(val ds: com.zaxxer.hikari.HikariDataSource, val hconf: com.zaxxer.hikari.HikariConfig) extends JdbcDataSource {
def createConnection(): Connection = ds.getConnection()
def close(): Unit = ds.close()
}
object HikariCPJdbcDataSource extends JdbcDataSourceFactory {
import com.zaxxer.hikari._
def forConfig(c: Config, driver: Driver, name: String, classLoader: ClassLoader): HikariCPJdbcDataSource = {
if(driver ne null)
throw new SlickException("An explicit Driver object is not supported by HikariCPJdbcDataSource")
val hconf = new HikariConfig()
// Connection settings
hconf.setDataSourceClassName(c.getStringOr("dataSourceClass", null))
Option(c.getStringOr("driverClassName", c.getStringOr("driver"))).map(hconf.setDriverClassName _)
hconf.setJdbcUrl(c.getStringOr("url", null))
c.getStringOpt("user").foreach(hconf.setUsername)
c.getStringOpt("password").foreach(hconf.setPassword)
c.getPropertiesOpt("properties").foreach(hconf.setDataSourceProperties)
// Pool configuration
hconf.setConnectionTimeout(c.getMillisecondsOr("connectionTimeout", 1000))
hconf.setValidationTimeout(c.getMillisecondsOr("validationTimeout", 1000))
hconf.setIdleTimeout(c.getMillisecondsOr("idleTimeout", 600000))
hconf.setMaxLifetime(c.getMillisecondsOr("maxLifetime", 1800000))
hconf.setLeakDetectionThreshold(c.getMillisecondsOr("leakDetectionThreshold", 0))
hconf.setInitializationFailFast(c.getBooleanOr("initializationFailFast", false))
c.getStringOpt("connectionTestQuery").foreach { s =>
hconf.setJdbc4ConnectionTest(false)
hconf.setConnectionTestQuery(s)
}
c.getStringOpt("connectionInitSql").foreach(hconf.setConnectionInitSql)
val numThreads = c.getIntOr("numThreads", 20)
hconf.setMaximumPoolSize(c.getIntOr("maxConnections", numThreads * 5))
hconf.setMinimumIdle(c.getIntOr("minConnections", numThreads))
hconf.setPoolName(name)
hconf.setRegisterMbeans(c.getBooleanOr("registerMbeans", false))
// Equivalent of ConnectionPreparer
hconf.setReadOnly(c.getBooleanOr("readOnly", false))
c.getStringOpt("isolation").map("TRANSACTION_" + _).foreach(hconf.setTransactionIsolation)
hconf.setCatalog(c.getStringOr("catalog", null))
val ds = new HikariDataSource(hconf)
new HikariCPJdbcDataSource(ds, hconf)
}
}
/** Set parameters on a new Connection. This is used by [[DataSourceJdbcDataSource]]. */
class ConnectionPreparer(c: Config) extends (Connection => Unit) {
val isolation = c.getStringOpt("isolation").map {
View
@@ -259,6 +259,7 @@
apilinks = {
'api': 'api/index.html#%s',
'codegenapi': 'codegen-api/index.html#%s',
'hikaricpapi': 'hikaricp-api/index.html#%s',
'scalaapi': 'http://www.scala-lang.org/api/'+scala_version+'/#%s'
}
@@ -22,6 +22,8 @@ on the Typesafe web site.
.. index:: Maven, sbt, artifacts, build, dependency
.. _dependencies:
Adding Slick to Your Project
============================
@@ -58,8 +60,10 @@ log output.
The Reactive Streams API is pulled in automatically as a transitive dependency.
If you want to use Slick's connection pool support, you need to add HikariCP_
as a dependency.
If you want to use Slick's connection pool support for HikariCP_, you need to add
the ``slick-hikaricp`` module as a dependency in the same way as shown for ``slick``
above. It will automatically provide a compatible version of HikariCP as a transitive
dependency.
Quick Introduction
==================
@@ -7,8 +7,9 @@ API Documentation (scaladoc)
----------------------------
* `Slick Core <api/index.html>`_ (slick)
* `TestKit <testkit-api/index.html>`_ (slick-testkit)
* `Code Generator <codegen-api/index.html>`_ (slick-codegen)
* `HikariCP integration <hikaricp-api/index.html>`_ (slick-hikaricp)
* `TestKit <testkit-api/index.html>`_ (slick-testkit)
* `Slick Extensions <extensions-api/index.html>`_ (slick-extensions)
User Manual
Oops, something went wrong.

0 comments on commit c197fdb

Please sign in to comment.