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

Fallback for Postgresql DB in AsyncSession #518

Open
8 tasks done
junoriosity opened this issue Dec 23, 2022 · 2 comments
Open
8 tasks done

Fallback for Postgresql DB in AsyncSession #518

junoriosity opened this issue Dec 23, 2022 · 2 comments
Labels
question Further information is requested

Comments

@junoriosity
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker

primary_engine = create_async_engine("PRIMARY_DATABASE_URL", echo=False, future=True)
replica_engine = create_async_engine("REPLICA_DATABASE_URL", echo=False, future=True)


async def get_primary_session() -> AsyncSession:
    async_session = sessionmaker(primary_engine, class_=AsyncSession, expire_on_commit=False)
    async with async_session() as session:
        yield session

async def get_replica_session() -> AsyncSession:
    async_session = sessionmaker(replica_engine, class_=AsyncSession, expire_on_commit=False)
    async with async_session() as session:
        yield session

Description

I am currently having two Postgresql Clusters attached to my Python FastAPI server (see the source code).

Now assume, that the replica crashes. What would be the best strategy to redirect the traffic back to the primary?

I currently have tried this:

async def get_replica_session() -> AsyncSession:
    try:
        async_session = sessionmaker(replica_engine, class_=AsyncSession, expire_on_commit=False)
        async with async_session() as session:
            yield session
    except:
        async_session = sessionmaker(primary_engine, class_=AsyncSession, expire_on_commit=False)
        async with async_session() as session:
            yield session

However, it used to throw Runtime errors? Hence, it would be great if you could give me some thoughts about it.

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.8

Python Version

3.10

Additional Context

No response

@junoriosity junoriosity added the question Further information is requested label Dec 23, 2022
@antont
Copy link

antont commented Jan 6, 2023

This is not about SQLModel actually but SQLAlchemy, your example also literally only imports sqlalchemy libs. Maybe you'll have better luck asking there or in StackOverflow with sqlalchemy tag.

Am also interested in the answer though :)

@sparklog
Copy link

sparklog commented Feb 3, 2024

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker

primary_engine = create_async_engine("PRIMARY_DATABASE_URL", echo=False, future=True)
replica_engine = create_async_engine("REPLICA_DATABASE_URL", echo=False, future=True)


async def get_primary_session() -> AsyncSession:
    async_session = sessionmaker(primary_engine, class_=AsyncSession, expire_on_commit=False)
    async with async_session() as session:
        yield session

async def get_replica_session() -> AsyncSession:
    async_session = sessionmaker(replica_engine, class_=AsyncSession, expire_on_commit=False)
    async with async_session() as session:
        yield session

Description

I am currently having two Postgresql Clusters attached to my Python FastAPI server (see the source code).

Now assume, that the replica crashes. What would be the best strategy to redirect the traffic back to the primary?

I currently have tried this:

async def get_replica_session() -> AsyncSession:
    try:
        async_session = sessionmaker(replica_engine, class_=AsyncSession, expire_on_commit=False)
        async with async_session() as session:
            yield session
    except:
        async_session = sessionmaker(primary_engine, class_=AsyncSession, expire_on_commit=False)
        async with async_session() as session:
            yield session

However, it used to throw Runtime errors? Hence, it would be great if you could give me some thoughts about it.

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.8

Python Version

3.10

Additional Context

No response

I have tried to use asyncSession which import from sqlalchemy, and then the problem solved. I hope it is useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants