Skip to content

Issue with transaction handling #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions core/src/main/scala/scalikejdbc/async/AsyncDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scalikejdbc.async
import scala.concurrent._
import scala.util.{ Failure, Success }
import scalikejdbc.async.ShortenedNames._
import scalikejdbc.async.internal.AsyncConnectionCommonImpl

/**
* Basic Database Accessor
Expand Down Expand Up @@ -54,14 +55,20 @@ object AsyncDB {
AsyncConnectionPool().borrow().toNonSharedConnection().map { nonSharedConnection =>
TxAsyncDBSession(nonSharedConnection)
}.flatMap { tx =>
tx.begin().flatMap { _ =>
op.apply(tx).andThen {
case Success(_) => tx.commit()
case Failure(e) => tx.rollback()
}.andThen {
case _ => tx.release()
}
val p = Promise[A]()
val connection = tx.connection.asInstanceOf[AsyncConnectionCommonImpl].underlying

connection.inTransaction(_ => op.apply(tx)).onComplete {
case Success(result) =>
tx.release()
p.success(result)
case Failure(e) =>
// As documentation recommends - close connection after rollback
connection.disconnect
tx.release()
p.failure(e)
}
p.future
}
}

Expand Down