Skip to content

Commit

Permalink
Fix: cursor handling for multiple cursors in a single transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneBakkerCineca committed Feb 20, 2023
1 parent 360573d commit 022b162
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lwetl/jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def __init__(self, login: str, auto_commit=False, upper_case=True):
self.counter = 0
self.cursors = [] # type: List[CursorStorage]
self.current_cursor = None
self.keep_current_cursor = False

# noinspection PyBroadException
def __del__(self):
Expand Down Expand Up @@ -495,6 +496,7 @@ def close(self, cursor: Union[Cursor, str, None] = None):
else:
if cursor == self.current_cursor:
self.current_cursor = None
self.keep_current_cursor = False
self.remove_cursor(cursor)
# noinspection PyBroadException
try:
Expand Down Expand Up @@ -547,19 +549,23 @@ def string2java_string(sql_or_list):
elif not isinstance(sql, str):
raise TypeError('Query (sql) must be a string.')

if (self.current_cursor is None) and (len(self.cursors) > 0):
self.current_cursor = self.cursors[-1].cursor
if self.current_cursor is None:
available_cursors = [cs.cursor for cs in self.cursors if not cs.keep]
if len(available_cursors) > 0:
self.current_cursor = available_cursors[-1].cursor
self.keep_current_cursor = False

if cursor is None:
if use_current_cursor:
if use_current_cursor and (not keep_cursor) and (not self.keep_current_cursor):
cursor = self.current_cursor
elif not self.has_cursor(cursor):
cursor = None
if cursor is None:
self.counter += 1
cursor = self.connection.cursor()
self.cursors.append(CursorStorage(cursor, keep_cursor))
self.current_cursor = cursor
self.current_cursor = cursor
self.keep_current_cursor = keep_cursor

while sql.strip().endswith(';'):
sql = sql.strip()[:-1]
Expand Down
2 changes: 1 addition & 1 deletion lwetl/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _insert_or_update(self, sql, parameters=None):
if self.commit_mode in [UPLOAD_MODE_COMMIT, UPLOAD_MODE_ROLLBACK]:
exec_error = None
try:
self.cursor = self.jdbc.execute(sql, parameters, self.cursor)
self.cursor = self.jdbc.execute(sql, parameters, self.cursor, use_current_cursor=False)
n = self.cursor.rowcount
# except DatabaseError as db_error:
except Exception as db_error:
Expand Down
2 changes: 1 addition & 1 deletion lwetl/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.7.9.master-d4b9e026cc8c7325b8ba212c49fedda57245d81d'
__version__ = '0.7.10.master-360573dffd10a32c255ad20ed3be5669b211124a'

0 comments on commit 022b162

Please sign in to comment.