Skip to content

Commit

Permalink
docs: describe DB API and transactions retry mechanism (googleapis#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaFaer authored Oct 13, 2022
1 parent ab768e4 commit 30a0666
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,31 @@ if any of the records does not already exist.
)
Connection API
--------------
Connection API represents a wrap-around for Python Spanner API, written in accordance with PEP-249, and provides a simple way of communication with a Spanner database through connection objects:

.. code:: python
from google.cloud.spanner_dbapi.connection import connect
connection = connect("instance-id", "database-id")
connection.autocommit = True
cursor = connection.cursor()
cursor.execute("SELECT * FROM table_name")
result = cursor.fetchall()
Aborted Transactions Retry Mechanism
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In ``!autocommit`` mode, transactions can be aborted due to transient errors. In most cases retry of an aborted transaction solves the problem. To simplify it, connection tracks SQL statements, executed in the current transaction. In case the transaction aborted, the connection initiates a new one and re-executes all the statements. In the process, the connection checks that retried statements are returning the same results that the original statements did. If results are different, the transaction is dropped, as the underlying data changed, and auto retry is impossible.

Auto-retry of aborted transactions is enabled only for ``!autocommit`` mode, as in ``autocommit`` mode transactions are never aborted.


Next Steps
~~~~~~~~~~

Expand Down

0 comments on commit 30a0666

Please sign in to comment.