|
| 1 | +.. _autocommit: |
| 2 | + |
| 3 | +Autocommit |
| 4 | +========== |
| 5 | + |
| 6 | +These examples are meant to highlight best practices and allow users to explore the |
| 7 | +difference between having ``AUTOCOMMIT`` enabled versus disabled. |
| 8 | + |
| 9 | +Throughout our examples, we use ``engine.begin()``. Code inside of a with-block using |
| 10 | +``engine.begin()`` will behave the same whether ``AUTOCOMMIT`` |
| 11 | +is enabled (``y``) or disabled (``n``). This is because ``engine.begin()`` automatically |
| 12 | +issues a ``COMMIT`` when the block successfully finished or a ``ROLLBACK`` if it fails. |
| 13 | +For any basic database modifications, the recommended best practice is to use |
| 14 | +``engine.begin()``. This is to ensure that the objects are in the desired state before |
| 15 | +performing further manipulation steps. While it is possible to use ``engine.connect()`` |
| 16 | +instead, when ``AUTOCOMMIT`` is disabled, you must manually, inside of the with-block, |
| 17 | +call ``commit()``. For a more nuanced discussion, see :ref:`begin_or_connect`. |
| 18 | + |
| 19 | +To explore what changes when ``AUTOCOMMIT`` is set to ``n``, you can: |
| 20 | + |
| 21 | +1. Modify your configuration file, as described on :ref:`example_configuration`, to set |
| 22 | + ``"AUTOCOMMIT":"n"``. |
| 23 | +2. Modify the code in the examples to use ``engine.connect()`` instead of |
| 24 | + ``engine.begin()``. |
| 25 | +3. Try to execute the examples and see what happens. You should get failures, as |
| 26 | + ``engine.connect()`` without ``AUTOCOMMIT`` requires you to manually call |
| 27 | + ``conn.commit()`` as needed. |
| 28 | +4. Add ``conn.commit()`` to the examples and try to execute them. They should now pass. |
| 29 | + |
| 30 | +.. note:: |
| 31 | + |
| 32 | + The examples on this page are presented in sections, but they are all part |
| 33 | + of the same module ``examples/features/specific_focuses/_1_autocommit.py``. |
| 34 | + They are broken up here to provide further information and context. Please |
| 35 | + ensure that you read and execute the parts in order. |
| 36 | + |
| 37 | + |
| 38 | +.. _ddl: |
| 39 | + |
| 40 | +Data Definition Language (DDL) |
| 41 | +------------------------------ |
| 42 | + |
| 43 | +.. literalinclude:: ../../../../examples/features/specific_focuses/_1_autocommit.py |
| 44 | + :language: python3 |
| 45 | + :caption: examples/features/specific_focuses/autocommit.py |
| 46 | + :end-before: # 2. Data Query Language (DQL) |
| 47 | + |
| 48 | + |
| 49 | +It is recommended to use ``engine.begin()`` for DDL statements. |
| 50 | + |
| 51 | +.. note:: |
| 52 | + |
| 53 | + Note that ``IDENTITY`` |
| 54 | + (`an Exasol Database keyword <https://docs.exasol.com/db/latest/sql_references/data_types/identitycolumns.htm?Highlight=identity>`__) |
| 55 | + **must** be included for the ID to autoincrement. This is not required for the |
| 56 | + examples given in :ref:`examples_non_orm` or :ref:`examples_orm`, where SQLAlchemy |
| 57 | + generates the required SQL statement for the Exasol database instance. |
| 58 | + |
| 59 | +Data Query Language (DQL) |
| 60 | +------------------------- |
| 61 | + |
| 62 | +.. literalinclude:: ../../../../examples/features/specific_focuses/_1_autocommit.py |
| 63 | + :language: python3 |
| 64 | + :caption: examples/features/specific_focuses/autocommit.py |
| 65 | + :start-at: # 2. Data Query Language (DQL) |
| 66 | + :end-before: # 3. Data Manipulation Language (DML) |
| 67 | + |
| 68 | +Typically, ``SELECT`` is considered the only member of DQL. In such use cases, no data |
| 69 | +manipulations are occurring, so no manual commits need to be made. As such, |
| 70 | +this code works and should stay the same regardless of whether ``AUTOCOMMIT`` is |
| 71 | +enabled or disabled. |
| 72 | + |
| 73 | +Data Manipulation Language (DML) |
| 74 | +-------------------------------- |
| 75 | + |
| 76 | +.. literalinclude:: ../../../../examples/features/specific_focuses/_1_autocommit.py |
| 77 | + :language: python3 |
| 78 | + :caption: examples/features/specific_focuses/autocommit.py |
| 79 | + :start-at: # 3. Data Manipulation Language (DML) |
| 80 | + |
| 81 | +Like the :ref:`ddl` example, it is preferred here to use ``begin()`` in the with-block. |
| 82 | +This behaves the same whether ``AUTOCOMMIT`` is enabled or disabled. For a |
| 83 | +more nuanced discussion, see :ref:`begin_or_connect`. |
| 84 | + |
| 85 | +.. _begin_or_connect: |
| 86 | + |
| 87 | + |
| 88 | +Connection Management in SQLAlchemy |
| 89 | +----------------------------------- |
| 90 | + |
| 91 | +.. _begin-method: |
| 92 | + |
| 93 | +The ``begin()`` Method |
| 94 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 95 | + |
| 96 | +The ``begin()`` method provides a **transactional boundary**. It is the safest choice |
| 97 | +for an operation that modifies the database. |
| 98 | + |
| 99 | +.. list-table:: When to use ``begin()`` |
| 100 | + :widths: 30 70 |
| 101 | + :header-rows: 1 |
| 102 | + |
| 103 | + * - Scenario |
| 104 | + - Description |
| 105 | + * - DDL |
| 106 | + - Use for ``CREATE``, ``ALTER``, or ``DROP`` commands to ensure schema changes are committed. |
| 107 | + * - DML |
| 108 | + - Ideal for ``INSERT``, ``UPDATE``, and ``DELETE`` where you want an "all-or-nothing" result. |
| 109 | + * - Auto-Rollback |
| 110 | + - If your code fails mid-block, SQLAlchemy automatically reverts any partial changes. |
| 111 | + |
| 112 | +.. _connect-method: |
| 113 | + |
| 114 | +The ``connect()`` Method |
| 115 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 116 | + |
| 117 | +The ``connect()`` method provides a **raw connection**. It offers granular control but |
| 118 | +places the responsibility of transaction management on the user. |
| 119 | + |
| 120 | +When ``AUTOCOMMIT`` is enabled, using ``connect()`` ensures that each statement is |
| 121 | +finalised by the database as soon as it is executed. If ``AUTOCOMMIT`` is disabled, |
| 122 | +then you must manually use ``commit``. |
| 123 | + |
| 124 | +.. list-table:: When to use ``connect()`` |
| 125 | + :widths: 30 70 |
| 126 | + :header-rows: 1 |
| 127 | + |
| 128 | + * - Scenario |
| 129 | + - Description |
| 130 | + * - DQL |
| 131 | + - Best for ``SELECT`` statements where no data is being changed. |
| 132 | + * - DML |
| 133 | + - Use if you need to commit specific parts of a long-running script at different times. |
| 134 | + * - Performance Tuning |
| 135 | + - Avoids the overhead of starting a transaction block when only reading data. |
0 commit comments