Skip to content

Commit

Permalink
More docs
Browse files Browse the repository at this point in the history
  • Loading branch information
devkral committed Dec 2, 2024
1 parent c742de3 commit 73a8d72
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
27 changes: 18 additions & 9 deletions docs/connections-and-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ available.
This means you have to be careful when using `iterate` to not open another connection via `connection` (except you set `force_rollback` to False before opening the connection).
Otherwise it will deadlock.

Example for `force_rollback`:

```python
{!> ../docs_src/connections/force_rollback.py !}
```

## Transactions

Transactions are lazily initialized. You dont't need a connected database to create them via `database.transaction()`.
Expand All @@ -49,15 +55,6 @@ Whenever the transaction is activated, a connected database is required.
A second way to get a transaction is via the `Connection`. It has also a `transaction()` method.


### Auto-rollback (`force_rollback`)

Transactions support an keyword parameter named `force_rollback` which default to `False`.
When set to `True` at the end of the transaction a rollback is tried.
This means all changes are undone.

This is a simpler variant of `force_rollback` of the database object.


### The three ways to use a transaction

#### Via the async context manager protocol
Expand Down Expand Up @@ -87,6 +84,18 @@ For a lower-level transaction API:
{!> ../docs_src/transactions/manually.py !}
```

### Auto-rollback (`force_rollback`)

Transactions support an keyword parameter named `force_rollback` which default to `False`.
When set to `True` at the end of the transaction a rollback is tried.
This means all changes are undone.

This is a simpler variant of `force_rollback` of the database object.
```python
{!> ../docs_src/transactions/force_rollback_transaction.py !}
```


### Isolation level

The state of a transaction is liked to the connection used in the currently executing async task.
Expand Down
15 changes: 15 additions & 0 deletions docs_src/connections/force_rollback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from databasez import Database

database = Database("sqlite:///foo.sqlite", force_rollback=True)


def test_foo():
async with database:
...
# do the tests
# and now everything is rolled back

async with database:
...
# do the tests
# and now everything is rolled back again
15 changes: 15 additions & 0 deletions docs_src/transactions/force_rollback_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from databasez import Database

database = Database("sqlite:///foo.sqlite")


def test_foo():
async with database:
await database.execute(...)

async with database.transaction(force_rollback=True):
# this is rolled back
await database.execute(...)
async with database.transaction(force_rollback=False):
...
# this is saved

0 comments on commit 73a8d72

Please sign in to comment.