Browse Source

akka-http use hikaricp connection pool (#2319)

Maxim Fedorov 8 years ago
parent
commit
a2b4116dd1

+ 1 - 1
frameworks/Scala/akka-http/build.sbt

@@ -12,7 +12,7 @@ libraryDependencies ++= Seq(
   "com.typesafe.akka" %% "akka-http-experimental" % "2.4.9",
   "com.typesafe.akka" %% "akka-http-spray-json-experimental" % "2.4.9",
   "mysql" % "mysql-connector-java" % "5.1.38",
-  "org.apache.commons" % "commons-dbcp2" % "2.1",
+  "com.zaxxer" % "HikariCP" % "2.5.1",
   "org.scalatra.scalate" %% "scalate-core" % "1.7.0",
   "org.scalatest" %% "scalatest" % "2.2.4" % "test"
 )

+ 2 - 4
frameworks/Scala/akka-http/src/main/resources/application.conf

@@ -13,11 +13,9 @@ akka {
         dbuser: "benchmarkdbuser"
         dbpass: "benchmarkdbpass"
         jdbc-url: "jdbc:mysql://0.0.0.0:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
-        min-idle: 30
-        max-idle: 30
-        max-total: -1
+        connection-pool-size: 100
         thread-pool-size: 100
       }
     }
   }
-}
+}

+ 7 - 19
frameworks/Scala/akka-http/src/main/scala/com/typesafe/akka/http/benchmark/datastore/MySqlDataStore.scala

@@ -7,9 +7,7 @@ import java.util.concurrent.Executors
 import akka.actor.ActorSystem
 import com.typesafe.akka.http.benchmark.entity.{Fortune, World}
 import com.typesafe.config.Config
-import org.apache.commons.dbcp2.{DriverManagerConnectionFactory, PoolableConnection, PoolableConnectionFactory, PoolingDataSource}
-import org.apache.commons.pool2.impl.GenericObjectPool
-
+import com.zaxxer.hikari._
 import scala.concurrent.{ExecutionContext, Future, Promise}
 
 class MySqlDataStore(components: {
@@ -18,23 +16,13 @@ class MySqlDataStore(components: {
 }) extends DataStore {
   val config = components.config.getConfig("akka.http.benchmark.mysql")
 
-  private val dataSource: PoolingDataSource[PoolableConnection] = {
-    val jdbcUrl = config.getString("jdbc-url")
-    val props = new Properties()
-    props.setProperty("user", config.getString("dbuser"))
-    props.setProperty("password", config.getString("dbpass"))
-
-    val connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, props)
-    val poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null)
-    val connectionPool = new GenericObjectPool[PoolableConnection](poolableConnectionFactory)
-    connectionPool.setTestOnBorrow(true)
-    connectionPool.setMinIdle(config.getString("min-idle").toInt)
-    connectionPool.setMaxIdle(config.getString("max-idle").toInt)
-    connectionPool.setMaxTotal(config.getString("max-total").toInt)
-    poolableConnectionFactory.setPool(connectionPool)
-    poolableConnectionFactory.setValidationQuery("select 1")
-    new PoolingDataSource[PoolableConnection](connectionPool)
+  private val dataSource = new HikariDataSource {
+    setJdbcUrl(config.getString("jdbc-url"))
+    setUsername(config.getString("dbuser"))
+    setPassword(config.getString("dbpass"))
+    setMaximumPoolSize(config.getInt("connection-pool-size"))
   }
+
   private implicit val executionContext: ExecutionContext = {
     val size = config.getInt("thread-pool-size")
     val threadPool = Executors.newFixedThreadPool(size)