summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/mysql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-02-12 18:07:41 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2012-02-12 18:07:41 -0500
commitd101c597f57bfa7a72636b8e76a14d5c9f82bfd4 (patch)
treecfa685294da543c57fec0ee2edd9b09710f86ff8 /lib/sqlalchemy/dialects/mysql
parentd50ea3eabf49a8f881a4a21dbafd471bd6510ba8 (diff)
downloadsqlalchemy-d101c597f57bfa7a72636b8e76a14d5c9f82bfd4.tar.gz
- [feature] Added support for the "isolation_level"
parameter to all MySQL dialects. Thanks to mu_mind for the patch here. [ticket:2394] - add documentation examples for mysql, postgresql - pep8ing
Diffstat (limited to 'lib/sqlalchemy/dialects/mysql')
-rw-r--r--lib/sqlalchemy/dialects/mysql/base.py51
1 files changed, 50 insertions, 1 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py
index 6aa250d2d..d0dd28e70 100644
--- a/lib/sqlalchemy/dialects/mysql/base.py
+++ b/lib/sqlalchemy/dialects/mysql/base.py
@@ -84,6 +84,23 @@ all lower case both within SQLAlchemy as well as on the MySQL
database itself, especially if database reflection features are
to be used.
+Transaction Isolation Level
+---------------------------
+
+:func:`.create_engine` accepts an ``isolation_level``
+parameter which results in the command ``SET SESSION
+TRANSACTION ISOLATION LEVEL <level>`` being invoked for
+every new connection. Valid values for this parameter are
+``READ COMMITTED``, ``READ UNCOMMITTED``,
+``REPEATABLE READ``, and ``SERIALIZABLE``::
+
+ engine = create_engine(
+ "mysql://scott:tiger@localhost/test",
+ isolation_level="READ UNCOMMITTED"
+ )
+
+(new in 0.7.6)
+
Keys
----
@@ -1768,8 +1785,40 @@ class MySQLDialect(default.DefaultDialect):
_backslash_escapes = True
_server_ansiquotes = False
- def __init__(self, use_ansiquotes=None, **kwargs):
+ def __init__(self, use_ansiquotes=None, isolation_level=None, **kwargs):
default.DefaultDialect.__init__(self, **kwargs)
+ self.isolation_level = isolation_level
+
+ def on_connect(self):
+ if self.isolation_level is not None:
+ def connect(conn):
+ self.set_isolation_level(conn, self.isolation_level)
+ return connect
+ else:
+ return None
+
+ _isolation_lookup = set(['SERIALIZABLE',
+ 'READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ'])
+
+ def set_isolation_level(self, connection, level):
+ level = level.replace('_', ' ')
+ if level not in self._isolation_lookup:
+ raise exc.ArgumentError(
+ "Invalid value '%s' for isolation_level. "
+ "Valid isolation levels for %s are %s" %
+ (level, self.name, ", ".join(self._isolation_lookup))
+ )
+ cursor = connection.cursor()
+ cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL %s" % level)
+ cursor.execute("COMMIT")
+ cursor.close()
+
+ def get_isolation_level(self, connection):
+ cursor = connection.cursor()
+ cursor.execute('SELECT @@tx_isolation')
+ val = cursor.fetchone()[0]
+ cursor.close()
+ return val.upper().replace("-", " ")
def do_commit(self, connection):
"""Execute a COMMIT."""