On Sep 15, 2013, at 6:46 AM, Felix Zumstein <[email protected]> wrote:

> All samples for the ORM part use a session to connect to the database, 
> whereas all samples in the Core part use a connection. If I use both 
> approaches in my app, does it make sense to execute the SQL Expression 
> Language (Core) through session.execute() or is there a reason I should use 
> connection.execute()?
> 
> For example, can I say:
> 
> from sqlalchemy.sql import select
> from models import User
> from main import session_scope
> 
> users = User.__table__
> 
> with session_scope() as session:
>     result = session.execute(select([users]))
> 
> 
> session_scope() is a context manager as described here.

what's going on with connections is that they represent some kind of ongoing 
state within a transaction (or not).   If you were to just say 
engine.execute(), or connection.execute() where you didn't begin() a 
transaction, then your statement executes on an arbitrary connection and 
autocommits.  If OTOH you use Session.execute(), you're using the Connection 
that specifically is associated with that Session, which normally is also 
ongoing within a transaction (you can get at this Connection by saying 
Session.connection() also).   What's important here is if you want your 
statement to occur along with the same transaction as all the other operations 
you're doing (usually you do).

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to