Skip to content

Instantly share code, notes, and snippets.

@p3t0r
Created March 14, 2011 21:46
Show Gist options
  • Save p3t0r/869950 to your computer and use it in GitHub Desktop.
Save p3t0r/869950 to your computer and use it in GitHub Desktop.
trait Retrying[T] extends Repository[T] {
abstract override def withConnection(f: (Connection => Option[T])): Option[T] = retry(f)
private def retry(f: (Connection => Option[T]), times: Int = 3): Option[T] = {
try {
println("number of tries left: %d".format(times))
super.withConnection(f)
} catch {
case _ if times == 0 => None
case _ => retry(f, times - 1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment