LoginSignup
0
1

More than 5 years have passed since last update.

Playframework + ScalikeJDBCでコネクションプールを使用する

Last updated at Posted at 2016-09-15

注意

!!!自分用メモ!!!

正しいやり方かどうかはわからない

困ったこと

DBs.setupAll をやらないと

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[IllegalStateException: Connection pool is not yet
 initialized.(name:'default)]]

となるので、どこでやるべきなのか迷ったけど、Moduleを使って起動時にやればいいのかなと。

ソースコード

build.sbt

  // scalikejdbc
  "org.scalikejdbc" %% "scalikejdbc"                  % "2.4.2",
  "org.scalikejdbc" %% "scalikejdbc-config"           % "2.4.2",
  "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.5.1",

application.conf

play.modules {
  # By default, Play will load any class called Module that is defined
  # in the root package (the "app" directory), or you can define them
  # explicitly below.
  # If there are any built-in modules that you want to disable, you can list them here.
  #enabled += my.application.Module

  enabled += "org.flywaydb.play.PlayModule"
  enabled += "modules.DBInitializerModule"

  # If there are any built-in modules that you want to disable, you can list them here.
  #disabled += ""
  disabled += "play.api.db.DBModule"
}

db {
  # You can declare as many datasources as you want.
  # By convention, the default datasource is named `default`

  default.driver = com.mysql.cj.jdbc.Driver
  default.url = "jdbc:mysql://localhost:3306/ticketman?characterEncoding=utf8&serverTimezone=UTC"
  default.username = root
  default.password = ""

  default.poolInitialSize=10
  default.poolMaxSize=10
  default.poolValidationQuery="select 1 as one"
}

Module

package modules

import com.google.inject.AbstractModule
import play.api.Logger
import scalikejdbc.config.DBs

/**
 * Created by Fumiyasu on 2016/09/15.
 */
class DBInitializer {
  DBs.setupAll()
  Logger.debug(s"db setup...")
}

class DBInitializerModule extends AbstractModule {
  def configure() = {
    bind(classOf[DBInitializer]).asEagerSingleton()
  }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1