Codebase list python-cx-oracle / 9e2744a doc / src / user_guide / txn_management.rst
9e2744a

Tree @9e2744a (Download .tar.gz)

txn_management.rst @9e2744aview markup · raw · history · blame

Transaction Management

A database transaction is a grouping of SQL statements that make a logical data change to the database.

When :meth:`Cursor.execute()` executes a SQL statement, a transaction is started or continued. By default, cx_Oracle does not commit this transaction to the database. The methods :meth:`Connection.commit()` and :meth:`Connection.rollback()` methods can be used to explicitly commit or rollback a transaction:

System Message: INFO/1 (<string>, line 10)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 10); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 10)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 10); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 10)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 10); backlink

Unknown interpreted text role "meth".
cursor.execute("INSERT INTO mytab (name) VALUES ('John')")
connection.commit()

When a database connection is closed, such as with :meth:`Connection.close()`, or when variables referencing the connection go out of scope, any uncommitted transaction will be rolled back.

System Message: INFO/1 (<string>, line 21)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 21); backlink

Unknown interpreted text role "meth".

Autocommitting

An alternative way to commit is to set the attribute :attr:`~Connection.autocommit` of the connection to True. This ensures all :ref:`DML <dml>` statements (INSERT, UPDATE etc) are committed as they are executed. Unlike :meth:`Connection.commit()`, this does not require an additional :ref:`round-trip <roundtrips>` to the database so it is more efficient when used appropriately.

System Message: INFO/1 (<string>, line 29)

No role entry for "attr" in module "docutils.parsers.rst.languages.en". Trying "attr" as canonical role name.

System Message: ERROR/3 (<string>, line 29); backlink

Unknown interpreted text role "attr".

System Message: INFO/1 (<string>, line 29)

No role entry for "ref" in module "docutils.parsers.rst.languages.en". Trying "ref" as canonical role name.

System Message: ERROR/3 (<string>, line 29); backlink

Unknown interpreted text role "ref".

System Message: INFO/1 (<string>, line 29)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 29); backlink

Unknown interpreted text role "meth".

System Message: INFO/1 (<string>, line 29)

No role entry for "ref" in module "docutils.parsers.rst.languages.en". Trying "ref" as canonical role name.

System Message: ERROR/3 (<string>, line 29); backlink

Unknown interpreted text role "ref".

Note that irrespective of the autocommit value, Oracle Database will always commit an open transaction when a DDL statement is executed.

When executing multiple DML statements that constitute a single transaction, it is recommended to use autocommit mode only for the last DML statement in the sequence of operations. Unnecessarily committing causes extra database load, and can destroy transactional consistency.

The example below shows a new customer being added to the table CUST_TABLE. The corresponding SALES table is updated with a purchase of 3000 pens from the customer. The final insert uses autocommit mode to commit both new records:

# Add a new customer
idVar = cursor.var(int)
connection.autocommit = False  # make sure any previous value is off
cursor.execute("""
        INSERT INTO cust_table (name) VALUES ('John')
        RETURNING id INTO :bvid""", bvid=idVar)

# Add sales data for the new customer and commit all new values
idVal = idVar.getvalue()[0]
connection.autocommit = True
cursor.execute("INSERT INTO sales_table VALUES (:bvid, 'pens', 3000)",
        bvid=idVal)

Explicit Transactions

The method :meth:`Connection.begin()` can be used to explicitly start a local or global transaction.

System Message: INFO/1 (<string>, line 68)

No role entry for "meth" in module "docutils.parsers.rst.languages.en". Trying "meth" as canonical role name.

System Message: ERROR/3 (<string>, line 68); backlink

Unknown interpreted text role "meth".

Without parameters, this explicitly begins a local transaction; otherwise, this explicitly begins a distributed (global) transaction with the given parameters. See the Oracle documentation for more details.

Note that in order to make use of global (distributed) transactions, the attributes :attr:`Connection.internal_name` and :attr:`Connection.external_name` attributes must be set.

System Message: INFO/1 (<string>, line 75)

No role entry for "attr" in module "docutils.parsers.rst.languages.en". Trying "attr" as canonical role name.

System Message: ERROR/3 (<string>, line 75); backlink

Unknown interpreted text role "attr".

System Message: INFO/1 (<string>, line 75)

No role entry for "attr" in module "docutils.parsers.rst.languages.en". Trying "attr" as canonical role name.

System Message: ERROR/3 (<string>, line 75); backlink

Unknown interpreted text role "attr".

Docutils System Messages

System Message: INFO/1 (<string>, line 1)

Hyperlink target "txnmgmnt" is not referenced.