PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Bagaimana cara melakukan transaksi database dengan psycopg2/python db api?

Gunakan db.set_isolation_level(n) , dengan asumsi db adalah objek koneksi Anda. Seperti yang ditulis Federico di sini, arti dari n adalah:

0 -> autocommit
1 -> read committed
2 -> serialized (but not officially supported by pg)
3 -> serialized

Seperti yang didokumentasikan di sini, psycopg2.extensions memberi Anda konstanta simbolis untuk tujuan:

Setting transaction isolation levels
====================================

psycopg2 connection objects hold informations about the PostgreSQL `transaction
isolation level`_.  The current transaction level can be read from the
`.isolation_level` attribute.  The default isolation level is ``READ
COMMITTED``.  A different isolation level con be set through the
`.set_isolation_level()` method.  The level can be set to one of the following
constants, defined in `psycopg2.extensions`:

`ISOLATION_LEVEL_AUTOCOMMIT`
    No transaction is started when command are issued and no
    `.commit()`/`.rollback()` is required.  Some PostgreSQL command such as
    ``CREATE DATABASE`` can't run into a transaction: to run such command use
    `.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)`.

`ISOLATION_LEVEL_READ_COMMITTED`
    This is the default value.  A new transaction is started at the first
    `.execute()` command on a cursor and at each new `.execute()` after a
    `.commit()` or a `.rollback()`.  The transaction runs in the PostgreSQL
    ``READ COMMITTED`` isolation level.

`ISOLATION_LEVEL_SERIALIZABLE`
    Transactions are run at a ``SERIALIZABLE`` isolation level.


.. _transaction isolation level: 
   http://www.postgresql.org/docs/8.1/static/transaction-iso.html


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Fungsi jendela Postgres dan kelompokkan dengan pengecualian

  2. Cara Menyebarkan Canvas LMS yang Sangat Tersedia dengan Cluster Database PostgreSQL

  3. Bagaimana cara saya mendapatkan dukungan LISTEN/NOTIFY asynchronous/even-driven di Java menggunakan database Postgres?

  4. bagaimana cara awalan string sebelum urutan yang dihasilkan oleh postgresql?

  5. Pilih Postgresql hingga jumlah total tertentu tercapai