Commit 018f1105 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

ZMySQLDA: automatically switch isolation level by SELECT query or not.

parent c60fc5a5
...@@ -107,7 +107,9 @@ ...@@ -107,7 +107,9 @@
!<em>isolation_level</em> at the begining of the connection string !<em>isolation_level</em> at the begining of the connection string
will set the transaction isolation level in each transaction. The will set the transaction isolation level in each transaction. The
value should be one of REPEATABLE-READ, READ-COMMITTED, value should be one of REPEATABLE-READ, READ-COMMITTED,
READ-UNCOMMITTED or SERIALIZABLE. READ-UNCOMMITTED or SERIALIZABLE. If not specified, REPEATABLE-READ
is used for if the first query in a Zope transaction is SELECT query,
otherwise READ-COMMITTED is used.
</dd> </dd>
<dd> <dd>
Transactions are highly recommended. Using a named lock in Transactions are highly recommended. Using a named lock in
......
...@@ -301,7 +301,7 @@ class DB(TM): ...@@ -301,7 +301,7 @@ class DB(TM):
FIELD_TYPE.TINY: "i", FIELD_TYPE.YEAR: "i", FIELD_TYPE.TINY: "i", FIELD_TYPE.YEAR: "i",
} }
_p_oid=_p_changed=_registered=None _p_oid=_p_changed=_registered=_current_isolation_level=None
def __del__(self): def __del__(self):
if self.db is not None: if self.db is not None:
...@@ -439,7 +439,6 @@ class DB(TM): ...@@ -439,7 +439,6 @@ class DB(TM):
def query(self, query_string, max_rows=1000): def query(self, query_string, max_rows=1000):
"""Execute 'query_string' and return at most 'max_rows'.""" """Execute 'query_string' and return at most 'max_rows'."""
self._use_TM and self._register()
desc = None desc = None
if isinstance(query_string, six.text_type): if isinstance(query_string, six.text_type):
query_string = query_string.encode('utf-8') query_string = query_string.encode('utf-8')
...@@ -448,6 +447,17 @@ class DB(TM): ...@@ -448,6 +447,17 @@ class DB(TM):
# Unfortunately, MySQLdb does not want to be graceful. # Unfortunately, MySQLdb does not want to be graceful.
if query_string[-1:] == b';': if query_string[-1:] == b';':
query_string = query_string[:-1] query_string = query_string[:-1]
if self._use_TM and not self._registered:
if self._isolation_level:
self._current_isolation_level = self._isolation_level
else:
for qs in query_string.split(b'\0'):
if match_select(qs.strip()):
self._current_isolation_level = 'REPEATABLE-READ'
break
else:
self._current_isolation_level = 'READ-COMMITTED'
self._register()
for qs in query_string.split(b'\0'): for qs in query_string.split(b'\0'):
qs = qs.strip() qs = qs.strip()
if qs: if qs:
...@@ -491,8 +501,8 @@ class DB(TM): ...@@ -491,8 +501,8 @@ class DB(TM):
try: try:
self._transaction_begun = True self._transaction_begun = True
if self._transactions: if self._transactions:
if self._isolation_level: if self._current_isolation_level:
self._query("SET TRANSACTION ISOLATION LEVEL %s" % self._isolation_level.replace('-', ' ')) self._query("SET TRANSACTION ISOLATION LEVEL %s" % self._current_isolation_level.replace('-', ' '))
self._query("BEGIN", allow_reconnect=True) self._query("BEGIN", allow_reconnect=True)
if self._mysql_lock: if self._mysql_lock:
self._query("SELECT GET_LOCK('%s',0)" % self._mysql_lock, allow_reconnect=not self._transactions) self._query("SELECT GET_LOCK('%s',0)" % self._mysql_lock, allow_reconnect=not self._transactions)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment