Skip to content

Comments

Refactor stores to use one database connection per query#9

Open
jochenklar wants to merge 1 commit intomainfrom
fix-stores
Open

Refactor stores to use one database connection per query#9
jochenklar wants to merge 1 commit intomainfrom
fix-stores

Conversation

@jochenklar
Copy link
Member

Replaces #8. Should be a bit more robust and less complex. I think the performance loss by using separate connections is not relevant to use, Django uses one connection per request by default.

@jochenklar jochenklar requested a review from MyPyDavid January 29, 2026 15:41
@MyPyDavid
Copy link
Member

MyPyDavid commented Jan 29, 2026

Does that with MySQLdb.connect(**config.STORE_CONNECTION) actually work?

PS Ok, the MySQLdb.connect has these context methods in it:

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

and what about potential rollbacks?

from contextlib import contextmanager

class BaseStore:
    @contextmanager
    def get_cursor_from_connect(self, connect_fn):
        """
        Open a new connection using connect_fn(), yield a cursor,
        commit on success, rollback on exception, and always close.
        """
        conn = connect_fn()
        cur = conn.cursor()
        try:
            yield cur
            try:
                conn.commit()
            except Exception:
                # Some drivers may be autocommit or not support commit -> ignore
                pass
        except Exception:
            try:
                conn.rollback()
            except Exception:
                pass
            raise
        finally:
            try:
                cur.close()
            except Exception:
                pass
            try:
                conn.close()
            except Exception:
                pass

eg in mysql store:

class MysqlStore(BaseStore):

    def connect(self):
        return MySQLdb.connect(**config.STORE_CONNECTION)

    def create_table(self):
        with self.get_cursor_from_connect(self.connect) as cur:
            cur.execute("""
                CREATE TABLE IF NOT EXISTS history (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    user_identifier VARCHAR(150),
                    project_id INT,
                    messages JSON,
                    created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                    UNIQUE KEY unique_user_project (user_identifier, project_id)
                );
            """)
            

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.

2 participants