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

Add env vars for setting transaction isolation level for both dbs #264

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions arxiv/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import importlib.metadata
from typing import Optional, List, Tuple
import os
from sqlalchemy.engine.interfaces import IsolationLevel
from secrets import token_hex
from urllib.parse import urlparse
from pydantic import BaseSettings, SecretStr
Expand Down Expand Up @@ -171,5 +172,7 @@ class Settings(BaseSettings):
CLASSIC_DB_URI: str = DEFAULT_DB
LATEXML_DB_URI: str = DEFAULT_LATEXML_DB
ECHO_SQL: bool = False
CLASSIC_DB_TRANSACTION_ISOLATION_LEVEL: Optional[IsolationLevel] = None
LATEXML_DB_TRANSACTION_ISOLATION_LEVEL: Optional[IsolationLevel] = None

settings = Settings()
18 changes: 12 additions & 6 deletions arxiv/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
with transaction() as session:
session.add(...)
"""
from typing import Generator
from typing import Optional
import logging
from contextlib import contextmanager

Expand All @@ -41,7 +41,7 @@

from sqlalchemy import create_engine, MetaData, String
from sqlalchemy.orm import sessionmaker, scoped_session, DeclarativeBase

from sqlalchemy.engine.interfaces import IsolationLevel

from ..config import settings

Expand All @@ -56,9 +56,11 @@ class LaTeXMLBase(DeclarativeBase):
logger = logging.getLogger(__name__)

engine = create_engine(settings.CLASSIC_DB_URI,
echo=settings.ECHO_SQL)
echo=settings.ECHO_SQL,
isolation_level=settings.CLASSIC_DB_TRANSACTION_ISOLATION_LEVEL)
latexml_engine = create_engine(settings.LATEXML_DB_URI,
echo=settings.ECHO_SQL)
echo=settings.ECHO_SQL,
isolation_level=settings.LATEXML_DB_TRANSACTION_ISOLATION_LEVEL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False)

def _app_ctx_id () -> int:
Expand All @@ -75,9 +77,13 @@ def get_db ():
db.close()

@contextmanager
def transaction ():
def transaction (transaction_isolation_level: Optional[IsolationLevel] = None):
in_flask = True if has_app_context() else False
db = session if in_flask else SessionLocal()
db = session if in_flask else SessionLocal()
if transaction_isolation_level:
db.connection(execution_options={
'isolation_level': transaction_isolation_level
})
try:
yield db

Expand Down
Loading