-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,367 additions
and
970 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ testconn.script | |
.settings | ||
.classpath | ||
.cache-* | ||
.bsp |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: scala | ||
|
||
scala: | ||
- 2.12.10 | ||
- 2.13.4 | ||
|
||
script: "sbt clean coverage test" | ||
after_success: "sbt coverageReport coveralls" |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.3.8 | ||
sbt.version=1.4.3 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.1.0-M11") |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sss.db | ||
|
||
|
||
import java.util.concurrent.Executors | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object ExecutionContextHelper { | ||
|
||
implicit val synchronousExecutionContext = ExecutionContext.fromExecutor(task => task.run()) | ||
|
||
implicit val ioExecutionContext = ExecutionContext | ||
.fromExecutorService( | ||
Executors.newCachedThreadPool() | ||
) | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package sss.db | ||
import java.sql.Connection | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.Try | ||
|
||
case class TransactionContext(conn: Connection, implicit val ec: ExecutionContext) | ||
|
||
trait FutureTx[+T] extends (TransactionContext => Future[T]) { | ||
|
||
def flatMap[C](t: T => FutureTx[C]): FutureTx[C] = context => { | ||
import context.ec | ||
val g: Future[TransactionContext => Future[C]] = apply(context) map t | ||
g flatMap (_(context)) | ||
} | ||
|
||
def map[C](t: T => C): FutureTx[C] = context => { | ||
import context.ec | ||
apply(context) map t | ||
} | ||
|
||
@deprecated("You are about to filter a future?") | ||
def withFilter(f: T => Boolean): FutureTx[T] = context => { | ||
import context.ec | ||
apply(context) filter f | ||
} | ||
|
||
|
||
def andAfter[U](pf: PartialFunction[Try[T], U]): FutureTx[T] = context => { | ||
import context.ec | ||
apply(context).andThen(pf) | ||
} | ||
|
||
} | ||
|
||
object FutureTx { | ||
def unit[A](a: A): FutureTx[A] = conn => Future.successful(a) | ||
|
||
def sequence[T](seqT: Seq[FutureTx[T]]): FutureTx[Seq[T]] = { | ||
seqT.foldLeft[FutureTx[Seq[T]]](FutureTx.unit(Seq.empty[T])) { | ||
(acc,e) => acc.flatMap(sq => e.map (_ +: sq)) | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package sss.db | ||
|
||
import sss.ancillary.FutureOps.AwaitReady | ||
import sss.db.TxIsolationLevel.TxIsolationLevel | ||
|
||
import java.sql.Connection | ||
import javax.sql.DataSource | ||
import scala.concurrent.duration.{Duration, DurationInt} | ||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.{Failure, Success, Try} | ||
|
||
trait FutureTxExecutor { | ||
|
||
def execute[T](fTx: FutureTx[T], | ||
conn: Connection, | ||
isRollback: Boolean, | ||
ec: ExecutionContext, | ||
isolationLevel: Option[TxIsolationLevel]): Future[T] = { | ||
|
||
implicit val ecImplicit = ec | ||
|
||
Try { | ||
isolationLevel.foreach(l => conn.setTransactionIsolation(l.id)) | ||
fTx(TransactionContext(conn, ec)) | ||
} match { | ||
case Failure(e) => | ||
|
||
try conn.rollback() | ||
finally conn.close() | ||
|
||
Future.failed(e) | ||
|
||
case Success(result) => | ||
result map { r => | ||
try { | ||
if (isRollback) conn.rollback() | ||
else conn.commit() | ||
r | ||
} finally conn.close() | ||
|
||
} recoverWith { case e => | ||
|
||
try conn.rollback() | ||
finally conn.close() | ||
|
||
Future.failed[T](e) | ||
} | ||
|
||
} | ||
} | ||
|
||
def execute[T](fTx: FutureTx[T], runContext: RunContext, isRollback: Boolean): Future[T] = { | ||
execute(fTx, runContext.ds.getConnection, isRollback, runContext.ec, runContext.isolationLevel) | ||
} | ||
|
||
|
||
def executeSync[T](fTx: FutureTx[T], | ||
ds: DataSource, | ||
isRollback: Boolean, isolationLevel: Option[TxIsolationLevel]): Try[T] = { | ||
val ec = ExecutionContextHelper.synchronousExecutionContext | ||
execute(fTx, ds.getConnection, isRollback, ec, isolationLevel).toTry(1.second) | ||
} | ||
|
||
|
||
} | ||
|
||
object FutureTxExecutor extends FutureTxExecutor |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package sss.db | ||
|
||
object Main { | ||
|
||
def main(args: Array[String]): Unit = { | ||
val plan = for { | ||
i <- 0 to 10 | ||
if i > 100 | ||
j <- i to 100 | ||
} yield j | ||
|
||
println(plan) | ||
} | ||
} |
Oops, something went wrong.