Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Play Scala JDBC Config
package models
import javax.inject._
import akka.actor.ActorSystem
import play.api.libs.concurrent.CustomExecutionContext
/**
* This class is a pointer to an execution context configured to point to "database.dispatcher"
* in the "application.conf" file.
*/
@Singleton
class DatabaseExecutionContext @Inject()(system: ActorSystem) extends CustomExecutionContext(system, "database.dispatcher")
# Default database configuration using H2 database engine in a persistent mode
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:target/db"
# db connections = ((physical_core_count * 2) + effective_spindle_count)
# https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing
fixedConnectionPool = 9
database.dispatcher {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = ${fixedConnectionPool}
}
}
// https://github.com/playframework/play-samples/tree/2.8.x/play-scala-anorm-example
// https://www.playframework.com/documentation/2.8.x/AccessingAnSQLDatabase
import javax.inject.Inject
import play.api.db.Database
import scala.concurrent.Future
class ScalaApplicationDatabase @Inject() (db: Database, databaseExecutionContext: DatabaseExecutionContext) {
def updateSomething(): Unit = {
Future {
db.withConnection { conn =>
// do whatever you need with the db connection
}
}(databaseExecutionContext)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.