Skip to content

Commit eeb7836

Browse files
authored
feat: add connection variable for ignoring transaction warnings (googleapis#1249)
Adds a connection variable for ignoring transaction warnings. Also adds a **kwargs argument to the connect function. This will be used for further connection variables in the future. Fixes googleapis/python-spanner-sqlalchemy#494
1 parent ccae6e0 commit eeb7836

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

google/cloud/spanner_dbapi/connection.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ class Connection:
8989
committed by other transactions since the start of the read-only transaction. Commit or rolling back
9090
the read-only transaction is semantically the same, and only indicates that the read-only transaction
9191
should end a that a new one should be started when the next statement is executed.
92+
93+
**kwargs: Initial value for connection variables.
9294
"""
9395

94-
def __init__(self, instance, database=None, read_only=False):
96+
def __init__(self, instance, database=None, read_only=False, **kwargs):
9597
self._instance = instance
9698
self._database = database
9799
self._ddl_statements = []
@@ -117,6 +119,7 @@ def __init__(self, instance, database=None, read_only=False):
117119
self._batch_dml_executor: BatchDmlExecutor = None
118120
self._transaction_helper = TransactionRetryHelper(self)
119121
self._autocommit_dml_mode: AutocommitDmlMode = AutocommitDmlMode.TRANSACTIONAL
122+
self._connection_variables = kwargs
120123

121124
@property
122125
def spanner_client(self):
@@ -206,6 +209,10 @@ def _client_transaction_started(self):
206209
"""
207210
return (not self._autocommit) or self._transaction_begin_marked
208211

212+
@property
213+
def _ignore_transaction_warnings(self):
214+
return self._connection_variables.get("ignore_transaction_warnings", False)
215+
209216
@property
210217
def instance(self):
211218
"""Instance to which this connection relates.
@@ -398,9 +405,10 @@ def commit(self):
398405
if self.database is None:
399406
raise ValueError("Database needs to be passed for this operation")
400407
if not self._client_transaction_started:
401-
warnings.warn(
402-
CLIENT_TRANSACTION_NOT_STARTED_WARNING, UserWarning, stacklevel=2
403-
)
408+
if not self._ignore_transaction_warnings:
409+
warnings.warn(
410+
CLIENT_TRANSACTION_NOT_STARTED_WARNING, UserWarning, stacklevel=2
411+
)
404412
return
405413

406414
self.run_prior_DDL_statements()
@@ -418,9 +426,10 @@ def rollback(self):
418426
This is a no-op if there is no active client transaction.
419427
"""
420428
if not self._client_transaction_started:
421-
warnings.warn(
422-
CLIENT_TRANSACTION_NOT_STARTED_WARNING, UserWarning, stacklevel=2
423-
)
429+
if not self._ignore_transaction_warnings:
430+
warnings.warn(
431+
CLIENT_TRANSACTION_NOT_STARTED_WARNING, UserWarning, stacklevel=2
432+
)
424433
return
425434
try:
426435
if self._spanner_transaction_started and not self._read_only:
@@ -654,6 +663,7 @@ def connect(
654663
user_agent=None,
655664
client=None,
656665
route_to_leader_enabled=True,
666+
**kwargs,
657667
):
658668
"""Creates a connection to a Google Cloud Spanner database.
659669
@@ -696,6 +706,8 @@ def connect(
696706
disable leader aware routing. Disabling leader aware routing would
697707
route all requests in RW/PDML transactions to the closest region.
698708
709+
**kwargs: Initial value for connection variables.
710+
699711
700712
:rtype: :class:`google.cloud.spanner_dbapi.connection.Connection`
701713
:returns: Connection object associated with the given Google Cloud Spanner

tests/unit/spanner_dbapi/test_connection.py

+13
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ def test_commit_in_autocommit_mode(self, mock_warn):
300300
CLIENT_TRANSACTION_NOT_STARTED_WARNING, UserWarning, stacklevel=2
301301
)
302302

303+
@mock.patch.object(warnings, "warn")
304+
def test_commit_in_autocommit_mode_with_ignore_warnings(self, mock_warn):
305+
conn = self._make_connection(
306+
DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED,
307+
ignore_transaction_warnings=True,
308+
)
309+
assert conn._ignore_transaction_warnings
310+
conn._autocommit = True
311+
312+
conn.commit()
313+
314+
assert not mock_warn.warn.called
315+
303316
def test_commit_database_error(self):
304317
from google.cloud.spanner_dbapi import Connection
305318

0 commit comments

Comments
 (0)