We have an environment with the following libraries and versions;
Python 3.8Normally, we run on a single RDS Mysql but because of the high load we needed to add a read replica and we have following configurations;
SQLALCHEMY_DATABASE_URI = //DB URLBefore creating the read replica we would use db and session like;
from flask_sqlalchemy import SQLAlchemyAfter creating the read replica we have added a read_session like;
read_engine = db.get_engine(bind='read')and started use it like;
read_session.query(MyEntity).filter_by(id=self.id).all()but after this point, our application started to produce errors like Packet sequence number wrong - got x but expected y and Can't reconnect until invalid transaction is rolled back .
When I googled the error, I see that the reason of this error is multi-threading and it says every request should be handled in separate sessions but I wonder how it would work before creating the read replica. It seems SQLAlchemy uses the same session for all request because session is created with same way we did in SQLAlchemy instance. Related code in SQLAlchemy =>
def __init__(self, app=None, use_native_unicode=True, session_options=None, metadata=None, query_class=BaseQuery, model_class=Model):I wonder, what we did wrong and how we can create a session to use every where like SQLAlchemy did it?