Database connection management #253

Closed
opened 2026-02-26 21:51:10 -05:00 by sirdog3355 · 2 comments
sirdog3355 commented 2026-02-26 21:51:10 -05:00 (Migrated from github.com)

During a code review executed by Claude Opus 4.6, it advised that I have many open connections, that the channel link commands create database connections per command execution, and that I never close them.

Need to refactor to have more proper database connection handling.

During a code review executed by [Claude Opus 4.6](https://claude.ai/), it advised that I have many open connections, that the channel link commands create database connections per command execution, and that I never close them. Need to refactor to have more proper database connection handling.
sirdog3355 commented 2026-03-05 20:57:22 -05:00 (Migrated from github.com)

Alright. Some work has been done, and I've got a roadmap.

For ALL classes

Ensure that in the __init__ that the connection is closed once the table is created (if necessary).

Short lived?

Add a __enter__ and __exit__ method to the file.

def __enter__(self):
        self.connection = sqlite3.connect("endurabot.db")
        self.cursor = self.connection.cursor()
        return self
        
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.connection.close()

Then use with CLASS_NAME as db: to make checks with it. Eliminate all instances of declaring the relevant class at the top of files or commands.

Long lived?

Initiate the connection at bot start via main.py.

bot.CLASS_NAME = CLASS_NAME() 

Then call the connection using self.bot wherever it is needed.

Alright. Some work has been done, and I've got a roadmap. ### For ALL classes Ensure that in the `__init__` that the connection is closed once the table is created (if necessary). ### Short lived? Add a ``__enter__`` and ``__exit__`` method to the file. ```py def __enter__(self): self.connection = sqlite3.connect("endurabot.db") self.cursor = self.connection.cursor() return self def __exit__(self, exc_type, exc_val, exc_tb): self.connection.close() ``` Then use `with CLASS_NAME as db:` to make checks with it. Eliminate all instances of declaring the relevant class at the top of files or commands. ### Long lived? Initiate the connection at bot start via `main.py`. ```py bot.CLASS_NAME = CLASS_NAME() ``` Then call the connection using `self.bot` wherever it is needed.
sirdog3355 commented 2026-03-06 22:41:01 -05:00 (Migrated from github.com)

Addressed by #261. Documentation updated. Closing.

Addressed by #261. Documentation updated. Closing.
Sign in to join this conversation.
No description provided.