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

Context manager support with connection object #947

Merged
merged 1 commit into from
Jul 16, 2024
Merged

Context manager support with connection object #947

merged 1 commit into from
Jul 16, 2024

Conversation

Earammak
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
When working with IO, It is common to acquire some resource i.e open('file.txt') or ibm_db_dbi.connect(connection_str). These resources also have to manually closed i.e fp.close() or connection.close(). Developers may forget to clean up this resources and this could lead to resource leaks. Python solves this problem with context managers which automatically solve the problem of resource management and ensure proper cleanup. They provide a convenient and reliable way to allocate and release resources, such as connecting and disconnecting from a database.

Context managers could provide a concise and intuitive way to handle database connections and transactions, ensuring proper cleanup and reducing the risk of resource leaks. With context managers, developers can write cleaner and more readable code by encapsulating the database-related operations within a well-defined scope. Additionally, context managers can also enable automatic rollback of transactions in case of exceptions, ensuring data consistency and integrity.

Describe the solution you'd like
Context managers to be added to connection objects such that they can be used easily like:

with ibm_db_dbi.connect(connection_str) as connection:
cursor = connection.cursor()
cursor.execute('SELECT * from table')
result = cursor.fetchall()
This removes the need for manual cleanup of resources

Describe alternatives you've considered

Additional context


def __enter__(self) -> 'IBM_DBConnection':
"""Connect to DB2."""
self.__cxn = ibm_db.connect(self.__dsn, '', '')

Choose a reason for hiding this comment

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

Please put username and password 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.

Please put username and password here

Username and password will be there in DSN only.
For cataloged connection, we need to give database, username and password [ For this new test suite added for both cataloged and uncatalogued connection].

Choose a reason for hiding this comment

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

Ok. Then why are we taking username and password in the constructor then. I don't see those values being use anywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The use of username and password depends on what type of connection we are trying to.
When we go for an uncatalogued connection, we will pass an empty string only for username and password field.
Please refer to: https://github.com/ibmdb/python-ibmdb/wiki/APIs#ibm_dbconnect

Choose a reason for hiding this comment

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

ok

@bimalkjha bimalkjha closed this Jul 16, 2024
@bimalkjha bimalkjha reopened this Jul 16, 2024
@bimalkjha bimalkjha merged commit e7f40b2 into ibmdb:master Jul 16, 2024
12 checks passed
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.

3 participants