# Django DB Transactions

**URL:** <https://forum.djangoproject.com/t/django-db-transactions/24314>\
**Category:** Using the ORM\
**Created:** [October 5, 2023, 3:55am UTC](https://forum.djangoproject.com/t/django-db-transactions/24314 "2023-10-05T03:55:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiqYousuf](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ashiqyousuf/32/13661_2.png) [@ashiqYousuf](https://forum.djangoproject.com/u/ashiqYousuf)\
**Post date:** [October 5, 2023, 3:55am UTC](https://forum.djangoproject.com/t/django-db-transactions/24314/1 "2023-10-05T03:55:46Z")

</div>

Hello everyone, I’m facing some issues right now. Actually I’m working on a project and want to set different isolation levels for different transactions. Some transactions which I do not care about, I want to set ISOLATION LEVEL as read uncommitted , for some SERIALIZABLE and for some REPEATABLE READ.

I was checking docs, but i the section django transactions i could see anything like this. Is this supported in Django ? If so can you please let me know how to do it?  
Thanks

---

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [October 9, 2023, 5:40pm UTC](https://forum.djangoproject.com/t/django-db-transactions/24314/2 "2023-10-09T17:40:18Z")

</div>

Hello @ashiqYousuf,

I would suggest defining multiple databases in your `DATABASES` settings, each definition with a meaningful name and pointing to the same DB but passing different OPTIONS that configure the [isolation level](https://docs.djangoproject.com/en/4.2/ref/databases/#mysql-isolation-level).

So then your logic would [explicitely choose](https://docs.djangoproject.com/en/4.2/topics/db/multi-db/#manually-selecting-a-database) which database to use in each case.

Does this help?  
Natalia.

---

<div class="post-metadata">

**Author:** ![ashiqYousuf](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ashiqyousuf/32/13661_2.png) [@ashiqYousuf](https://forum.djangoproject.com/u/ashiqYousuf)\
**Post date:** [October 9, 2023, 6:04pm UTC](https://forum.djangoproject.com/t/django-db-transactions/24314/3 "2023-10-09T18:04:10Z")

</div>

Hi thanks for the response, but I want to know if I can do this within views, for certain queries like as

start transaction with isolation level read committed  
Query 1  
End transaction

start transaction with different isolation level  
Query 2  
End transaction

Like this.

Or for certain users having status = statisticians  
I want to set isolation level repeatable read, let for api /getdata ad I don’t want the thicks and thins while he/she is analysing data.

And for same api I want to set isolation level read umcomiited for a normal user.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [October 10, 2023, 3:29pm UTC](https://forum.djangoproject.com/t/django-db-transactions/24314/4 "2023-10-10T15:29:10Z")

</div>

There shouldn’t be a need to define multiple database connections. You can use [raw SQL](https://docs.djangoproject.com/en/4.2/topics/db/sql/#connections-and-cursors) to change the configured isloation level. For example, in a middleware [using MariaDB](https://mariadb.com/kb/en/set-transaction/) you might do:

```auto
if request.user.status == "statistician":
    level = "REPEATABLE READ"
else:
    level = "READ UNCOMMITTED"
with connection.cursor() as c:
    c.execute(f"SET SESSION TRANSACTION ISOLATION LEVEL {level}")
```

---

<div class="post-metadata">

**Author:** ![ashiqYousuf](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ashiqyousuf/32/13661_2.png) [@ashiqYousuf](https://forum.djangoproject.com/u/ashiqYousuf)\
**Post date:** [October 13, 2023, 3:04am UTC](https://forum.djangoproject.com/t/django-db-transactions/24314/5 "2023-10-13T03:04:39Z")

</div>

@adamchainz thanks for your response. I’ll surely look into it.

Yea that seems to be a straightforward solution.

---

<div class="post-metadata">

**Author:** ![emyller](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/emyller/32/31228_2.png) [@emyller](https://forum.djangoproject.com/u/emyller)\
**Post date:** [January 14, 2026, 2:08pm UTC](https://forum.djangoproject.com/t/django-db-transactions/24314/6 "2026-01-14T14:08:08Z")

</div>

I like Natalia’s solution because often the developer will need to handle transaction isolation level in more than one use case. Doing this on the fly, as suggested by other comments, will lead to transaction isolation issues that are more likely to arise, and harder to debug and fix. Explicitly selecting a connection to use is both safer and cleaner.
