O
O
Oleg Kotov2019-11-29 07:34:09
Python
Oleg Kotov, 2019-11-29 07:34:09

What is the difference between asyncpg.Connection.transaction and asyncpg.Connection.acquire?

Hello.
I don’t really rummage in the database, and I don’t understand what is the difference transaction()from acquire()
Judging by the asyncpg documentation, acquire()it allows you to pick up a connection from the pool (which is created in advance),
execute some code, and then return it back to the pool.
But what does it do then transaction()?
Well, that is, I understand that he opens a transaction. But what does this give us? What can we do rollback, but acquire()writes directly, without wrapping it into a transaction?
Both of them are used like this:

async with pool.transaction() as conn:
ИЛИ 
async with pool.acquire() as conn:
  result = conn.fetch(query)
  <or any code i want to execute>

And it seems like, in my code they are interchangeable. That is, it works this way and that way. (Why I actually got to understand, but I didn’t find an exhaustive answer)
Well, I also know that the transaction wraps the request in BEGIN;, COMMIT;.
Perhaps it acquire()should be used for read-only, but transaction()for create-update-delete?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2019-11-29
@AgeofCreations

Mixed in a bunch of horses, people

1. acquire is getting a connection
2. transaction is providing atomicity for several operations (and only creation and modification operations), compare this with acquire, the same as warm with soft. Well, without getting a connection, you won’t be able to start a transaction, and you won’t just make queries to the database.
3. "with pool.transaction() OR pool.acquire() as conn:" is generally brain inflammation
4. asyncpg asynchronous library, there is at least async with

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question