wheaties
wheaties

Reputation: 35980

SQLAlchemy: Effect of 2 session objects?

Will the following use 2 DB connections from the connection pool because of the useage of 2 session calls?

users_from_contacts = db.session.query(User).join(Email, Email.user_id == User.id).join(Contacts, Contacts.email_id = Email.id)
users_from_past = db.session.query(User).join(Group, Group.user_id == User.id)

all_users = users_from_contacts.union_all(users_from_past).all()

Upvotes: 0

Views: 61

Answers (1)

zzzeek
zzzeek

Reputation: 75297

no, for one because the Session runs in an ongoing, single-connection transaction unless autocommit is set to the non-default of True, and two because there is only one SQL query emitted from the code above, when the .all() method is called.

relevant docs:

http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#managing-transactions

http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#returning-lists-and-scalars

Upvotes: 1

Related Questions