Skip to content

Commit e8b5d8c

Browse files
committed
Merge pull request #18 from beenokle/develop
Issue with transaction handling
2 parents fb0ea74 + 5ad53fc commit e8b5d8c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

core/src/main/scala/scalikejdbc/async/AsyncDB.scala

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scalikejdbc.async
1818
import scala.concurrent._
1919
import scala.util.{ Failure, Success }
2020
import scalikejdbc.async.ShortenedNames._
21+
import scalikejdbc.async.internal.AsyncConnectionCommonImpl
2122

2223
/**
2324
* Basic Database Accessor
@@ -54,14 +55,20 @@ object AsyncDB {
5455
AsyncConnectionPool().borrow().toNonSharedConnection().map { nonSharedConnection =>
5556
TxAsyncDBSession(nonSharedConnection)
5657
}.flatMap { tx =>
57-
tx.begin().flatMap { _ =>
58-
op.apply(tx).andThen {
59-
case Success(_) => tx.commit()
60-
case Failure(e) => tx.rollback()
61-
}.andThen {
62-
case _ => tx.release()
63-
}
58+
val p = Promise[A]()
59+
val connection = tx.connection.asInstanceOf[AsyncConnectionCommonImpl].underlying
60+
61+
connection.inTransaction(_ => op.apply(tx)).onComplete {
62+
case Success(result) =>
63+
tx.release()
64+
p.success(result)
65+
case Failure(e) =>
66+
// As documentation recommends - close connection after rollback
67+
connection.disconnect
68+
tx.release()
69+
p.failure(e)
6470
}
71+
p.future
6572
}
6673
}
6774

0 commit comments

Comments
 (0)