Refactor stores to use one database connection per query#9
Open
jochenklar wants to merge 1 commit intomainfrom
Open
Refactor stores to use one database connection per query#9jochenklar wants to merge 1 commit intomainfrom
jochenklar wants to merge 1 commit intomainfrom
Conversation
Member
|
PS Ok, the 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:
passeg 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)
);
""")
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.