Skip to content

Instantly share code, notes, and snippets.

@semenodm
Created January 20, 2016 18:55
Show Gist options
  • Save semenodm/306da9fc7e0ca7d070b7 to your computer and use it in GitHub Desktop.
Save semenodm/306da9fc7e0ca7d070b7 to your computer and use it in GitHub Desktop.
My way to cope the asynchronus way of handling distributed transactions
import java.sql._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
implicit val ec: ExecutionContext = ???
val f1 = Future{
val connection : Connection = ???
//execute some business logic and write into datasource 1
//don't commit anything here
connection
}
val f2 = Future{
val connection : Connection = ???
//execute some business logic and write into datasource 2
//don't commit anything here
connection
}
val f3 = Future{
val connection : Connection = ???
//execute some business logic and write into datasource 3
//don't commit anything here
connection
}
val result = for (
con1 <- f1;
con2 <- f2;
con3 <- f3;
) yield List(con1, con2, con3)
result onComplete{
case Success(conns@(con1, con2, con3)) =>
conns.foreach(_.commit())
case Failure(t) => //no access to connections
// here since we've got exception
//conns.foreach(_.rollback())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment