问题
I'm using SQLAlchemy 0.6.4 on the Pylons 1.0 framework. I've tried every permutation of setting autoflush and autocommit to True and False, but I've found that SQLAlchemy wants to wrap all SQL sessions or writes with a BEGIN/COMMIT. I've configured the scoped_session in models/meta.py as below:
"""SQLAlchemy Metadata and Session object"""
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
__all__ = ['Base', 'Session']
# SQLAlchemy session manager. Updated by model.init_model()
Session = scoped_session(sessionmaker(autoflush=False, autocommit=True))
# The declarative Base
Base = declarative_base()
metadata = MetaData()
回答1:
If your problem is the slowness of this behaviour, the solution might be to begin/end transactions in some sensible way explicitly in your application.
回答2:
Does not look like it:
While many DBAPIs implement a flag called autocommit, the current SQLAlchemy behavior is such that it implements its own autocommit. This is achieved by detecting statements which represent data-changing operations, i.e. INSERT, UPDATE, DELETE, etc., and then issuing a COMMIT automatically if no transaction is in progress.
来源:https://stackoverflow.com/questions/4393035/is-there-a-way-to-have-sqlalchemy-not-wrap-sql-writes-within-a-begin-and-commit