diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-01-16 17:04:07 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-01-16 17:04:07 -0500 |
commit | 7325ba60bce50c63ce83fcb44f6b337664262ad0 (patch) | |
tree | 3ead541a555e1fda9f57173ab836d60334a9e3b1 /lib/sqlalchemy/engine/base.py | |
parent | 8259e2fd2bb183bdcbc019bd03a281f411c80307 (diff) | |
download | sqlalchemy-7325ba60bce50c63ce83fcb44f6b337664262ad0.tar.gz |
- execution_options() on Connection accepts
"isolation_level" argument, sets transaction isolation
level for that connection only until returned to the
connection pool, for thsoe backends which support it
(SQLite, Postgresql) [ticket:2001]
- disallow the option on Engine (use isolation_level to create_engine()),
Executable (we don't want to check/set per statement)
- docs
Diffstat (limited to 'lib/sqlalchemy/engine/base.py')
-rw-r--r-- | lib/sqlalchemy/engine/base.py | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 1727e6905..8de2e2a3a 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -877,17 +877,45 @@ class Connection(Connectable): underlying resource, it is probably best to ensure that the copies would be discarded immediately, which is implicit if used as in:: - result = connection.execution_options(stream_results=True).\ + result = connection.execution_options(stream_results=True).\\ execute(stmt) - The options are the same as those accepted by - :meth:`sqlalchemy.sql.expression.Executable.execution_options`. + :meth:`.Connection.execution_options` accepts all options as those + accepted by :meth:`.Executable.execution_options`. Additionally, + it includes options that are applicable only to + :class:`.Connection`. + + :param isolation_level: Set the transaction isolation level for + the lifespan of this connection. Valid values include + those string values accepted by the ``isolation_level`` + parameter passed to :func:`.create_engine`, and are + database specific, including those for :ref:`sqlite_toplevel`, + :ref:`postgresql_toplevel` - see those dialect's documentation + for further info. + + Note that this option necessarily affects the underying + DBAPI connection for the lifespan of the originating + :class:`.Connection`, and is not per-execution. This + setting is not removed until the underying DBAPI connection + is returned to the connection pool, i.e. + the :meth:`.Connection.close` method is called. + + :param \**kw: All options accepted by :meth:`.Executable.execution_options` + are also accepted. """ c = self._clone() c._execution_options = c._execution_options.union(opt) + if 'isolation_level' in opt: + c._set_isolation_level() return c + def _set_isolation_level(self): + self.dialect.set_isolation_level(self.connection, + self._execution_options['isolation_level']) + self.connection._connection_record.finalize_callback = \ + self.dialect.reset_isolation_level + @property def closed(self): """Return True if this connection is closed.""" @@ -1724,6 +1752,13 @@ class Engine(Connectable, log.Identified): if proxy: interfaces.ConnectionProxy._adapt_listener(self, proxy) if execution_options: + if 'isolation_level' in execution_options: + raise exc.ArgumentError( + "'isolation_level' execution option may " + "only be specified on Connection.execution_options(). " + "To set engine-wide isolation level, " + "use the isolation_level argument to create_engine()." + ) self.update_execution_options(**execution_options) @@ -1736,7 +1771,6 @@ class Engine(Connectable, log.Identified): :meth:`Connection.execution_options` as well as :meth:`sqlalchemy.sql.expression.Executable.execution_options`. - """ self._execution_options = \ self._execution_options.union(opt) |