Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait JDBCer {
- this: AkkaAgentMySQLStateServiceComponent =>
- val retryTimes = config int "db.extensions.retries" or 5
- /**
- * Get a random slave connection name for Jeeves
- */
- def slaveDB(): Option[String] = {
- // Randomize List
- val random = Random.shuffle(mysqlStateService.getHealthyNodes())
- // Check for the number of available healthy connections to MySQL
- if (random.size > 0) {
- // Pick the first one (random item)
- val connectionName = random(0)
- // Log Debug
- please debug "Random Slave DB: " + connectionName
- // Return Random Slave Connection
- Option(connectionName)
- } else {
- // There are no healthy MySQL nodes available currently
- please warn "No healthy MySQL nodes available currently!"
- None
- }
- }
- /**
- * Issue DB query multiple times, stops when successfull
- */
- def retriableQuery[A](queryFunc: Connection => A): A = {
- (retryTimes times) attempt {
- slaveDB() match {
- case Some(dbName) => {
- DB.withConnection(dbName) {
- implicit connection =>
- queryFunc(connection)
- }
- }
- case _ => {
- throw new IllegalStateException("No healthy MySQL nodes available currently!")
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment