Skip to content

Feat/connection block #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Vats-shivam
Copy link
Collaborator

I have updated the Connection class with entry and exit method as well as removed the contextlib.closing method from the existing test suite below i have attached the test results
Screenshot 2025-07-25 161725

This basically involves with connection.py file and nuodb_date_time_test.py file

return self

def __exit__(self, exc_type, exc_val, exc_tb):
# type: () -> None
Copy link
Contributor

@adriansuarez adriansuarez Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Spacing is off (one too many spaces).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adriansuarez for catching that! I've fixed the spacing issue in the latest commit.

Copy link
Contributor

@madscientist madscientist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to update the documentation, as well. Most especially we need to document how the exit dunder method handles things when autocommit=False.

Probably we should add docstrings to enter and exit, at least.

Comment on lines 362 to 371
try:
if exc_type is None:
# No error: commit if needed
if not self.autocommit:
self.commit()
else:
# Error! Rollback if needed.
if not self.autocommit:
self.rollback()
finally:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the crux of the change. Whether we should commit automatically on exit or not.

My personal feeling is that we should not do this. If the user sets autocommit=False and they don't explicitly commit, then we should not commit "for" them... even when there's no exception.

Do we have any examples from other database's python drivers, to see how they handle this situation?

@rshaull do you have thoughts about this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two common patterns for how a connection context manager behaves:

Commit on exit – as seen in sqlite3, where exiting the with block commits any pending changes.

Rollback on exit – as seen in python-oracledb, where uncommitted changes are rolled back when the context closes.

Since there’s no universal standard, we need to decide which behavior aligns best with our use case.

Copy link
Contributor

@rshaull rshaull Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these changes, if the programmer explicitly closes a connection without an explicit commit or rollback, any uncommitted statements will be rolled back by the database. The same is true if they forget to explicitly close the connection.

We should preserve that behavior. There is no need to explicitly call rollback() when closing the connection. You should not call commit().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated exit_ block behavior without calling rollback or commit explicitly

@@ -350,3 +350,24 @@ def cursor(self, prepared_statement_cache_size=50):
"""
self._check_closed()
return cursor.Cursor(self.__session, prepared_statement_cache_size)

def __enter__(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're missing a # type comment here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added required comments and docstrings for documentation.

@madscientist
Copy link
Contributor

We do need to figure out why you're getting test failures. On my system the tests are all passing (for the current master branch HEAD).

@madscientist
Copy link
Contributor

As far as I can see this PR didn't trigger a circleci check. Did I miss it? I confess I'm not very familiar with this config, but when I push a change it automatically runs. Maybe we only have it set up to run when changes are made to this repo but not to any forks? @adriansuarez do you know how to configure this?

@adriansuarez
Copy link
Contributor

As far as I can see this PR didn't trigger a circleci check. Did I miss it? I confess I'm not very familiar with this config, but when I push a change it automatically runs. Maybe we only have it set up to run when changes are made to this repo but not to any forks? @adriansuarez do you know how to configure this?

I will manually kick off a run of our CircleCI pipeline. We have automatic runs triggered by fork PRs turned off to prevent someone from making a code change that exposes / uploads sensitive data like the NuoDB license, which is exported as an environment variable in the test setup. This change is obviously not doing anything like that.

@Vats-shivam Vats-shivam requested a review from madscientist July 29, 2025 18:11
@adriansuarez
Copy link
Contributor

@Vats-shivam FYI, the latest CI run failed with the error:

NuoDB Admin TLS support is DISABLED
Starting NuoDB Admin succeeded.
Traceback (most recent call last):
  File "/usr/lib64/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib64/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/opt/nuodb/etc/python/site-packages/pynuoadmin/nuocmd.py", line 7, in <module>
    from . import nuodb_cli
  File "/opt/nuodb/etc/python/site-packages/pynuoadmin/nuodb_cli.py", line 55, in <module>
    from . import nuodb_mgmt
  File "/opt/nuodb/etc/python/site-packages/pynuoadmin/nuodb_mgmt.py", line 29, in <module>
    from pynuodb.session import Session
  File "/home/nuodb/project/pynuodb/__init__.py", line 11, in <module>
    from .connection import *  # pylint: disable=wildcard-import
  File "/home/nuodb/project/pynuodb/connection.py", line 377
    self.close()
               ^
IndentationError: unindent does not match any outer indentation level

https://app.circleci.com/pipelines/github/nuodb/nuodb-python/150/workflows/c8e004f9-1be9-4bb7-836d-83cd0aa59c7c/jobs/146

@Vats-shivam
Copy link
Collaborator Author

@Vats-shivam FYI, the latest CI run failed with the error:

NuoDB Admin TLS support is DISABLED
Starting NuoDB Admin succeeded.
Traceback (most recent call last):
  File "/usr/lib64/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib64/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/opt/nuodb/etc/python/site-packages/pynuoadmin/nuocmd.py", line 7, in <module>
    from . import nuodb_cli
  File "/opt/nuodb/etc/python/site-packages/pynuoadmin/nuodb_cli.py", line 55, in <module>
    from . import nuodb_mgmt
  File "/opt/nuodb/etc/python/site-packages/pynuoadmin/nuodb_mgmt.py", line 29, in <module>
    from pynuodb.session import Session
  File "/home/nuodb/project/pynuodb/__init__.py", line 11, in <module>
    from .connection import *  # pylint: disable=wildcard-import
  File "/home/nuodb/project/pynuodb/connection.py", line 377
    self.close()
               ^
IndentationError: unindent does not match any outer indentation level

https://app.circleci.com/pipelines/github/nuodb/nuodb-python/150/workflows/c8e004f9-1be9-4bb7-836d-83cd0aa59c7c/jobs/146

@adriansuarez My bad, really sorry didn't tested last time.
Now it should be OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants