Skip to content
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

vdk-core: managed connection commit/rollback support #189

Merged
merged 4 commits into from
Sep 7, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ def connect(self) -> PEP249Connection:
self._db_con = db_con
return self._db_con

def cursor(self, *args, **kwargs):
if hasattr(self._db_con, "cursor"):
return ManagedCursor(self._db_con.cursor(*args, **kwargs), self._log)
return super().cursor()

def commit(self, *args, **kwargs):
if hasattr(self._db_con, "commit"):
return self._db_con.commit(*args, **kwargs)
return super().commit()

def rollback(self, *args, **kwargs):
if hasattr(self._db_con, "rollback"):
return self._db_con.rollback(*args, **kwargs)
return super().rollback()

def close(self, *args, **kwargs) -> None:
"""
Close database connection. No op if it is already closed.
"""
try:
if self._is_db_con_open:
self._log.debug(f"Closing database connection {self} ... ")
self._is_db_con_open = False
self._db_con.close()
self._log.debug("Closing database connection SUCCEEDED.")
except Exception as e:
self._log.exception(
"Closing database connection FAILED. No problem, I'm continuing as if nothing happened.",
e,
)

# @abstractmethod # inherit optionally
def _before_cursor_execute(self, cur: ManagedCursor) -> None:
pass
Expand Down Expand Up @@ -172,22 +203,6 @@ def __exit__(
def __del__(self) -> None:
self.close()

def close(self) -> None:
"""
Close database connection. No op if it is already closed.
"""
try:
if self._is_db_con_open:
self._log.debug(f"Closing database connection {self} ... ")
self._is_db_con_open = False
self._db_con.close()
self._log.debug("Closing database connection SUCCEEDED.")
except Exception as e:
self._log.exception(
"Closing database connection FAILED. No problem, I'm continuing as if nothing happened.",
e,
)

def __str__(self) -> str:
return "ManagedConnection[ isConnected:{} {} ]".format(
self._is_db_con_open, self._db_con
Expand Down