Skip to content

Commit 2b91491

Browse files
committed
add UseBlock documentation
1 parent a2cc53c commit 2b91491

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

scalasql/test/src/api/TransactionTests.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,27 @@ trait TransactionTests extends ScalaSqlSuite {
587587
}
588588
}
589589

590+
test("useBlock") - checker.recorded(
591+
"""
592+
Both `transaction` and `savepoint` accessors are actually returning a special `UseBlock[...]` types.
593+
When you call `transaction(use)` it desugars into `transaction.apply(use)` that provides a default
594+
resource-style management - it creates a transaction (or savepoint), runs `use` block immediately, then releases
595+
(commits or rolls back) the transaction.
596+
597+
If you need more control over the lifecycle, you may use transaction.allocate() method that returns
598+
`(resource, releaseFunction)` pair.
599+
This is especially useful when delaying side-effects with FP libraries like cats-effect or ZIO.
600+
**Important:** `allocate()` is impure and must be delayed in that case also.
601+
The `dbClient.transaction` expression itself does not perform any side-effects, it just creates closures.
602+
""",
603+
Text {
604+
val transactionBlock: scalasql.core.UseBlock[DbApi.Txn] = dbClient.transaction
605+
606+
def createTransactionWithReleseFunction(): (DbApi.Txn, Option[Throwable] => Unit) =
607+
transactionBlock.allocate()
608+
}
609+
)
610+
590611
test("listener") {
591612
test("beforeCommit and afterCommit are called under normal circumstances") {
592613
val listener = new StubTransactionListener()

0 commit comments

Comments
 (0)