📄 session.py
字号:
if type is None: try: self.commit() except: self.rollback() raise else: self.rollback()class Session(object): """Encapsulates a set of objects being operated upon within an object-relational operation. The Session is the front end to SQLAlchemy's **Unit of Work** implementation. The concept behind Unit of Work is to track modifications to a field of objects, and then be able to flush those changes to the database in a single operation. SQLAlchemy's unit of work includes these functions: * The ability to track in-memory changes on scalar- and collection-based object attributes, such that database persistence operations can be assembled based on those changes. * The ability to organize individual SQL queries and population of newly generated primary and foreign key-holding attributes during a persist operation such that referential integrity is maintained at all times. * The ability to maintain insert ordering against the order in which new instances were added to the session. * an Identity Map, which is a dictionary keying instances to their unique primary key identity. This ensures that only one copy of a particular entity is ever present within the session, even if repeated load operations for the same entity occur. This allows many parts of an application to get a handle to a particular object without any chance of modifications going to two different places. When dealing with instances of mapped classes, an instance may be *attached* to a particular Session, else it is *unattached* . An instance also may or may not correspond to an actual row in the database. These conditions break up into four distinct states: * *Transient* - an instance that's not in a session, and is not saved to the database; i.e. it has no database identity. The only relationship such an object has to the ORM is that its class has a `mapper()` associated with it. * *Pending* - when you `save()` a transient instance, it becomes pending. It still wasn't actually flushed to the database yet, but it will be when the next flush occurs. * *Persistent* - An instance which is present in the session and has a record in the database. You get persistent instances by either flushing so that the pending instances become persistent, or by querying the database for existing instances (or moving persistent instances from other sessions into your local session). * *Detached* - an instance which has a record in the database, but is not in any session. Theres nothing wrong with this, and you can use objects normally when they're detached, **except** they will not be able to issue any SQL in order to load collections or attributes which are not yet loaded, or were marked as "expired". The session methods which control instance state include ``save()``, ``update()``, ``save_or_update()``, ``delete()``, ``merge()``, and ``expunge()``. The Session object is **not** threadsafe, particularly during flush operations. A session which is only read from (i.e. is never flushed) can be used by concurrent threads if it's acceptable that some object instances may be loaded twice. The typical pattern to managing Sessions in a multi-threaded environment is either to use mutexes to limit concurrent access to one thread at a time, or more commonly to establish a unique session for every thread, using a threadlocal variable. SQLAlchemy provides a thread-managed Session adapter, provided by the [sqlalchemy.orm#scoped_session()] function. """ def __init__(self, bind=None, autoflush=True, transactional=False, twophase=False, echo_uow=False, weak_identity_map=True, binds=None, extension=None): """Construct a new Session. A session is usually constructed using the [sqlalchemy.orm#create_session()] function, or its more "automated" variant [sqlalchemy.orm#sessionmaker()]. autoflush When ``True``, all query operations will issue a ``flush()`` call to this ``Session`` before proceeding. This is a convenience feature so that ``flush()`` need not be called repeatedly in order for database queries to retrieve results. It's typical that ``autoflush`` is used in conjunction with ``transactional=True``, so that ``flush()`` is never called; you just call ``commit()`` when changes are complete to finalize all changes to the database. bind An optional ``Engine`` or ``Connection`` to which this ``Session`` should be bound. When specified, all SQL operations performed by this session will execute via this connectable. binds An optional dictionary, which contains more granular "bind" information than the ``bind`` parameter provides. This dictionary can map individual ``Table`` instances as well as ``Mapper`` instances to individual ``Engine`` or ``Connection`` objects. Operations which proceed relative to a particular ``Mapper`` will consult this dictionary for the direct ``Mapper`` instance as well as the mapper's ``mapped_table`` attribute in order to locate an connectable to use. The full resolution is described in the ``get_bind()`` method of ``Session``. Usage looks like:: sess = Session(binds={ SomeMappedClass : create_engine('postgres://engine1'), somemapper : create_engine('postgres://engine2'), some_table : create_engine('postgres://engine3'), }) Also see the ``bind_mapper()`` and ``bind_table()`` methods. echo_uow When ``True``, configure Python logging to dump all unit-of-work transactions. This is the equivalent of ``logging.getLogger('sqlalchemy.orm.unitofwork').setLevel(logging.DEBUG)``. extension An optional [sqlalchemy.orm.session#SessionExtension] instance, which will receive pre- and post- commit and flush events, as well as a post-rollback event. User- defined code may be placed within these hooks using a user-defined subclass of ``SessionExtension``. transactional Set up this ``Session`` to automatically begin transactions. Setting this flag to ``True`` is the rough equivalent of calling ``begin()`` after each ``commit()`` operation, after each ``rollback()``, and after each ``close()``. Basically, this has the effect that all session operations are performed within the context of a transaction. Note that the ``begin()`` operation does not immediately utilize any connection resources; only when connection resources are first required do they get allocated into a transactional context. twophase When ``True``, all transactions will be started using [sqlalchemy.engine_TwoPhaseTransaction]. During a ``commit()``, after ``flush()`` has been issued for all attached databases, the ``prepare()`` method on each database's ``TwoPhaseTransaction`` will be called. This allows each database to roll back the entire transaction, before each transaction is committed. weak_identity_map When set to the default value of ``False``, a weak-referencing map is used; instances which are not externally referenced will be garbage collected immediately. For dereferenced instances which have pending changes present, the attribute management system will create a temporary strong-reference to the object which lasts until the changes are flushed to the database, at which point it's again dereferenced. Alternatively, when using the value ``True``, the identity map uses a regular Python dictionary to store instances. The session will maintain all instances present until they are removed using expunge(), clear(), or purge(). """ self.echo_uow = echo_uow self.weak_identity_map = weak_identity_map self.uow = unitofwork.UnitOfWork(self) self.identity_map = self.uow.identity_map self.bind = bind self.__binds = {} self.transaction = None self.hash_key = id(self) self.autoflush = autoflush self.transactional = transactional self.twophase = twophase self.extension = extension self._query_cls = query.Query self._mapper_flush_opts = {} if binds is not None: for mapperortable, value in binds.iteritems(): if isinstance(mapperortable, type): mapperortable = _class_mapper(mapperortable).base_mapper self.__binds[mapperortable] = value if isinstance(mapperortable, Mapper): for t in mapperortable._all_tables: self.__binds[t] = value if self.transactional: self.begin() _sessions[self.hash_key] = self def begin(self, **kwargs): """Begin a transaction on this Session.""" if self.transaction is not None: self.transaction = self.transaction._begin(**kwargs) else: self.transaction = SessionTransaction(self, **kwargs) return self.transaction create_transaction = begin def begin_nested(self): """Begin a `nested` transaction on this Session. This utilizes a ``SAVEPOINT`` transaction for databases which support this feature. """ return self.begin(nested=True) def rollback(self): """Rollback the current transaction in progress. If no transaction is in progress, this method is a pass-thru. """ if self.transaction is None: pass else: self.transaction.rollback() # TODO: we can rollback attribute values. however # we would want to expand attributes.py to be able to save *two* rollback points, one to the # last flush() and the other to when the object first entered the transaction. # [ticket:705] #attributes.rollback(*self.identity_map.values()) if self.transaction is None and self.transactional: self.begin() def commit(self): """Commit the current transaction in progress. If no transaction is in progress, this method raises an InvalidRequestError. If the ``begin()`` method was called on this ``Session`` additional times subsequent to its first call, ``commit()`` will not actually commit, and instead pops an internal SessionTransaction off its internal stack of transactions. Only when the "root" SessionTransaction is reached does an actual database-level commit occur. """ if self.transaction is None: if self.transactional: self.begin() else: raise exceptions.InvalidRequestError("No transaction is begun.") self.transaction.commit() if self.transaction is None and self.transactional: self.begin() def prepare(self): """Prepare the current transaction in progress for two phase commit. If no transaction is in progress, this method raises an InvalidRequestError. Only root transactions of two phase sessions can be prepared. If the current transaction is not such, an InvalidRequestError is raised. """ if self.transaction is None: if self.transactional: self.begin() else: raise exceptions.InvalidRequestError("No transaction is begun.") self.transaction.prepare() def connection(self, mapper=None, **kwargs): """Return a ``Connection`` corresponding to this session's transactional context, if any. If this ``Session`` is transactional, the connection will be in the context of this session's transaction. Otherwise, the connection is returned by the ``contextual_connect()`` method on the engine. The `mapper` argument is a class or mapper to which a bound engine will be located; use this when the Session itself is either bound to multiple engines or connections, or is not bound to any connectable. \**kwargs are additional arguments which will be passed to get_bind(). See the get_bind() method for details. Note that the ``ShardedSession`` subclass takes a different get_bind() argument signature. """ return self.__connection(self.get_bind(mapper, **kwargs)) def __connection(self, engine, **kwargs): if self.transaction is not None: return self.transaction.get_or_add(engine) else: return engine.contextual_connect(**kwargs) def execute(self, clause, params=None, mapper=None, **kwargs): """Using the given mapper to identify the appropriate ``Engine`` or ``Connection`` to be used for statement execution, execute the given ``ClauseElement`` using the provided parameter dictionary. Return a ``ResultProxy`` corresponding to the execution's results. If this method allocates a new ``Connection`` for the operation, then the ``ResultProxy`` 's ``close()`` method will release the resources of the underlying ``Connection``. """ engine = self.get_bind(mapper, clause=clause, **kwargs) return self.__connection(engine, close_with_result=True).execute(clause, params or {}) def scalar(self, clause, params=None, mapper=None, **kwargs): """Like execute() but return a scalar result.""" engine = self.get_bind(mapper, clause=clause) return self.__connection(engine, close_with_result=True).scalar(clause, params or {}, **kwargs) def close(self):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -