Skip to content

Commit 1f74c57

Browse files
committed
making sure connection is back in pool before result returns
1 parent 6bcf40a commit 1f74c57

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

db-async-common/src/main/scala/com/github/mauricio/async/db/pool/AsyncObjectPool.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.github.mauricio.async.db.pool
1818

19-
import scala.concurrent.Future
19+
import scala.concurrent.{ExecutionContext, Future}
2020

2121
/**
2222
*
@@ -70,10 +70,12 @@ trait AsyncObjectPool[T] {
7070
* @return f wrapped with take and giveBack
7171
*/
7272

73-
def use[A](f : T => Future[A])(implicit executionContext : scala.concurrent.ExecutionContext) : Future[A] =
73+
def use[A](f: (T) => Future[A])(implicit executionContext: ExecutionContext): Future[A] =
7474
take.flatMap { item =>
75-
f(item).andThen { case _ =>
76-
giveBack(item)
75+
f(item) recoverWith {
76+
case error => giveBack(item).flatMap(_ => Future.failed(error))
77+
} flatMap { res =>
78+
giveBack(item).map { _ => res }
7779
}
7880
}
7981

0 commit comments

Comments
 (0)