Created
June 16, 2012 22:18
-
-
Save feliperazeek/2942668 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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!") | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment