How do I do database transactions with psycopg2/python db api?

后端 未结 3 1725
无人及你
无人及你 2020-12-07 23:29

Im fiddling with psycopg2 , and while there\'s a .commit() and .rollback() there\'s no .begin() or similar to start a transaction , or so it seems ? I\'d expect to be able t

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 23:30

    The BEGIN with python standard DB API is always implicit. When you start working with the database the driver issues a BEGIN and after any COMMIT or ROLLBACK another BEGIN is issued. A python DB API compliant with the specification should always work this way (not only the postgresql).

    You can change this setting the isolation level to autocommit with db.set_isolation_level(n) as pointed by Alex Martelli.

    As Tebas said the begin is implicit but not executed until an SQL is executed, so if you don't execute any SQL, the session is not in a transaction.

提交回复
热议问题