diff --git a/core/src/main/scala/io/github/gaelrenoux/tranzactio/DatabaseServiceBase.scala b/core/src/main/scala/io/github/gaelrenoux/tranzactio/DatabaseServiceBase.scala index 57abc72..ca9a52b 100644 --- a/core/src/main/scala/io/github/gaelrenoux/tranzactio/DatabaseServiceBase.scala +++ b/core/src/main/scala/io/github/gaelrenoux/tranzactio/DatabaseServiceBase.scala @@ -16,42 +16,33 @@ abstract class DatabaseServiceBase[Connection: Tag](connectionSource: Connection override def transaction[R, E, A](zio: => ZIO[Connection with R, E, A], commitOnFailure: => Boolean = false) (implicit errorStrategies: ErrorStrategiesRef, trace: Trace): ZIO[R, Either[DbException, E], A] = - ZIO.environmentWithZIO[R] { r => - runTransaction({ (c: JdbcConnection) => - connectionFromJdbc(c) - .map(r ++ ZEnvironment(_)) - .flatMap(zio.provideEnvironment(_)) - }, commitOnFailure) - } + runTransaction({ (c: JdbcConnection) => + connectionFromJdbc(c) + .flatMap { connection => zio.provideSomeEnvironment[R](_.union[Connection](ZEnvironment(connection))) } + // Note: cannot use the simpler `_ ++ ZEnvironment(connection)` instead of the union, because it doesn't compile in 2.12 + }, commitOnFailure) override def transactionOrDieStream[R, E, A](stream: => ZStream[Connection with R, E, A], commitOnFailure: => Boolean = false) (implicit errorStrategies: ErrorStrategiesRef, trace: Trace): ZStream[R, E, A] = - ZStream.environmentWithStream[R] { r => - runTransactionOrDieStream({ (c: JdbcConnection) => - ZStream.fromZIO(connectionFromJdbc(c)) - .map(r ++ ZEnvironment(_)) - .flatMap(stream.provideEnvironment(_)) - }, commitOnFailure) - } + runTransactionOrDieStream({ (c: JdbcConnection) => + ZStream + .fromZIO(connectionFromJdbc(c)) + .flatMap { connection => stream.provideSomeEnvironment[R](_.union[Connection](ZEnvironment(connection))) } + }, commitOnFailure) override def autoCommit[R, E, A](zio: => ZIO[Connection with R, E, A]) (implicit errorStrategies: ErrorStrategiesRef, trace: Trace): ZIO[R, Either[DbException, E], A] = - ZIO.environmentWithZIO[R] { r => - runAutoCommit { (c: JdbcConnection) => - connectionFromJdbc(c) - .map(r ++ ZEnvironment(_)) - .flatMap(zio.provideEnvironment(_)) - } + runAutoCommit { (c: JdbcConnection) => + connectionFromJdbc(c) + .flatMap { connection => zio.provideSomeEnvironment[R](_.union[Connection](ZEnvironment(connection))) } } override def autoCommitStream[R, E, A](stream: => ZStream[Connection with R, E, A]) (implicit errorStrategies: ErrorStrategiesRef, trace: Trace): ZStream[R, Either[DbException, E], A] = - ZStream.environmentWithStream[R] { r => - runAutoCommitStream { (c: JdbcConnection) => - ZStream.fromZIO(connectionFromJdbc(c)) - .map(r ++ ZEnvironment(_)) - .flatMap(stream.provideEnvironment(_)) - } + runAutoCommitStream { (c: JdbcConnection) => + ZStream + .fromZIO(connectionFromJdbc(c)) + .flatMap { connection => stream.provideSomeEnvironment[R](_.union[Connection](ZEnvironment(connection))) } } }