Created
April 21, 2020 04:45
-
-
Save dhaeb/4689edaa85f4809246bfd82dfc926534 to your computer and use it in GitHub Desktop.
Play Scala JDBC Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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