Reputation: 43
I am working on a Pyramid application that uses SQLAlchemy's ORM library and thus we use sessions. I am not clear on how sessions and connection pooling work together or if I even need connection pooling when using sessions.
Currently, here is how we create a session:
engine = create_engine(connection_string)
Session = sessionmaker(bind=engine)
I have modified creating the engine to add connection pooling:
pooled_engine = create_engine(connection_string, pool_size=20, max_overflow=5,echo=True, echo_pool=True)
Session = sessionmaker(bind=pooled_engine)
This seems to work (the app connects to the db and can read/write to the db), but I can't tell if connection pooling is being used or not. I tried logging the connection pooling code, but I don't see anything on the console. I do however see SQL statements which is due to the echo flag being set to True.
I am not understanding how sessions and connection pooling work together or why I am not seeing log statements when echo_pool is set to True.
Any help would be appreciated,
Thank-you
Upvotes: 4
Views: 1432
Reputation: 1496
The engine handles the connection pooling and it is enabled by default.
From the documentation:
The Engine returned by the create_engine() function in most cases has a QueuePool integrated, pre-configured with reasonable pooling defaults. If you’re reading this section only to learn how to enable pooling - congratulations! You’re already done.
I believe the Session objects do not correspond to database sessions. Rather they are ORM sessions. They do not actually even have a connection to the database unless they are querying or saving to the database. Note, I am not 100% sure on this, so if someone more knowledgeable about SQLAlchemy internals could confirm or edit this, that would be appreciated.
Upvotes: 2