You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before creating the read replica we would use db and session like;
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(session_options={"expire_on_commit": False})
db.session.query(MyEntity).filter_by(id=self.id).all()
After creating the read replica we have added a read_session like;
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 Flask-SQLAlchemy uses the same session for all request because session is created with same way we did in Flasck-SQLAlchemy instance. Related code in Flask-SQLAlchemy =>
def __init__(self, app=None, use_native_unicode=True, session_options=None,
metadata=None, query_class=BaseQuery, model_class=Model):
self.use_native_unicode = use_native_unicode
self.Query = query_class
self.session = self.create_scoped_session(session_options) # here is how session created in SQL Alchemy
self.Model = self.make_declarative_base(model_class, metadata)
self._engine_lock = Lock()
self.app = app
_include_sqlalchemy(self, query_class)
if app is not None:
self.init_app(app)
I wonder, what we did wrong and how we can create a session to use every where like Flask-SQLAlchemy did it?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
We have an environment with the following libraries and versions;
Normally, 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;
Before creating the read replica we would use db and session like;
After creating the read replica we have added a read_session like;
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
andCan'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 Flask-SQLAlchemy uses the same session for all request because session is created with same way we did in Flasck-SQLAlchemy instance. Related code in Flask-SQLAlchemy =>
I wonder, what we did wrong and how we can create a session to use every where like Flask-SQLAlchemy did it?
Beta Was this translation helpful? Give feedback.
All reactions