Created
January 20, 2016 18:55
-
-
Save semenodm/306da9fc7e0ca7d070b7 to your computer and use it in GitHub Desktop.
My way to cope the asynchronus way of handling distributed transactions
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
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