Skip to content

Commit 4286227

Browse files
Added support for locking SODA documents while fetching them (equivalent
of SQL select for update).
1 parent 6caccf9 commit 4286227

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

doc/src/api_manual/soda.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,29 @@ SodaOperation Methods
545545
19 from 19.11).
546546

547547

548+
.. method:: SodaOperation.lock()
549+
550+
Specifies whether the documents fetched from the collection should be
551+
locked (equivalent to SQL "select for update").
552+
553+
The next commit or rollback on the connection made after the operation is
554+
performed will "unlock" the documents. Ensure that the connection is not in
555+
autocommit mode or the documents will be unlocked immediately after the
556+
operation is complete.
557+
558+
This method should only be used with read operations (other than
559+
:func:`~SodaOperation.count()`) and should not be used in
560+
conjunction with non-terminal methods :meth:`~SodaOperation.skip()` and
561+
:meth:`~SodaOperation.limit()`.
562+
563+
If this method is specified in conjunction with a write operation this
564+
method is ignored.
565+
566+
This method is only supported in Oracle Client 21.3 and higher (also
567+
available in Oracle Client 19 from 19.11).
568+
569+
.. versionadded:: 1.4
570+
548571
.. method:: SodaOperation.key(value)
549572

550573
Specifies that the document with the specified key should be returned.

doc/src/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ Thick Mode Changes
6060

6161
#) Added function :meth:`SodaCollection.getIndexes()` for getting the indexes
6262
on a SODA collection.
63+
#) Added support for specifying if documents should be locked when fetched
64+
from SODA collections. A new non-terminal method
65+
:meth:`~SodaOperation.lock()` was added which requires Oracle Client
66+
21.3 or higher (or Oracle Client 19 from 19.11).
6367
#) Relaxed restriction for end-to-end tracing string connection
6468
attributes. These values can now be set to the value ``None`` which will be
6569
treated the same as an empty string.

src/oracledb/impl/thick/odpi.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ cdef extern from "impl/thick/odpi/embed/dpi.c":
445445
uint32_t skip
446446
uint32_t limit
447447
uint32_t fetchArraySize
448+
bint lock
448449

449450
ctypedef struct dpiStmtInfo:
450451
bint isQuery

src/oracledb/impl/thick/soda.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,4 +664,6 @@ cdef class ThickSodaOpImpl:
664664
options.limit = op._limit
665665
if op._fetch_array_size is not None:
666666
options.fetchArraySize = op._fetch_array_size
667+
if op._lock:
668+
options.lock = True
667669
return impl

src/oracledb/soda.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ def __init__(self, collection: SodaCollection) -> None:
463463
self._skip = None
464464
self._limit = None
465465
self._fetch_array_size = None
466+
self._lock = False
466467

467468
def count(self) -> int:
468469
"""
@@ -552,6 +553,18 @@ def hint(self, value: str) -> "SodaOperation":
552553
self._hint = value
553554
return self
554555

556+
def lock(self) -> "SodaOperation":
557+
"""
558+
Specifies whether the documents fetched from the collection should be
559+
locked (equivalent to SQL "select for update"). Use of this method
560+
requires Oracle Client 21.3 or higher (or Oracle Client 19 from 19.11).
561+
562+
As a convenience, the SodaOperation object is returned so that further
563+
criteria can be specified by chaining methods together.
564+
"""
565+
self._lock = True
566+
return self
567+
555568
def key(self, value: str) -> "SodaOperation":
556569
"""
557570
Specifies that the document with the specified key should be returned.

tests/test_3400_soda_collection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,5 +663,18 @@ def test_3429_getting_indexes(self):
663663
self.assertEqual(indexes[0]["fields"][0]["path"], "address.city")
664664
self.assertEqual(indexes[1]["fields"][0]["path"], "address.postal_code")
665665

666+
def test_3430_lock_documents(self):
667+
"3430 - test locking documents on fetch"
668+
soda_db = self.get_soda_database(minclient=(19, 11))
669+
coll = soda_db.createCollection("TestSodaLockDocs")
670+
coll.find().remove()
671+
values_to_insert = [
672+
{"name": "Bob", "age": 46},
673+
{"name": "Barb", "age": 45},
674+
{"name": "Sandy", "age": 47}
675+
]
676+
coll.insertMany(values_to_insert)
677+
coll.find().lock().getDocuments()
678+
666679
if __name__ == "__main__":
667680
test_env.run_test_cases()

0 commit comments

Comments
 (0)