Author: phd
Date: 2007-03-06 08:31:37 -0700 (Tue, 06 Mar 2007)
New Revision: 2403
Modified:
home/phd/SQLObject/paramstyles/sqlobject/mysql/mysqlconnection.py
Log:
Merged patches from the revisions 2393:2402 from the trunk
Modified: home/phd/SQLObject/paramstyles/sqlobject/mysql/mysqlconnection.py
===================================================================
--- home/phd/SQLObject/paramstyles/sqlobject/mysql/mysqlconnection.py 2007-03-06 15:30:43 UTC (rev 2402)
+++ home/phd/SQLObject/paramstyles/sqlobject/mysql/mysqlconnection.py 2007-03-06 15:31:37 UTC (rev 2403)
@@ -20,7 +20,7 @@
def __init__(self, db, user, password='', host='localhost', port=0, **kw):
global MySQLdb
if MySQLdb is None:
- import MySQLdb
+ import MySQLdb, MySQLdb.constants.CR, MySQLdb.constants.ER
self.module = MySQLdb
self.host = host
self.port = port
@@ -28,9 +28,7 @@
self.user = user
self.password = password
self.kw = {}
- if MySQLdb.version_info[0] > 1 or (MySQLdb.version_info[0] == 1 and \
- (MySQLdb.version_info[1] > 2 or \
- (MySQLdb.version_info[1] == 2 and MySQLdb.version_info[2] >= 1))):
+ if MySQLdb.version_info[:3] >= (1, 2, 1):
self.need_unicode = True
else:
self.need_unicode = False
@@ -62,10 +60,12 @@
try:
conn = self.module.connect(host=self.host, port=self.port,
db=self.db, user=self.user, passwd=self.password, **self.kw)
+ if MySQLdb.version_info[:3] >= (1, 2, 2):
+ conn.ping(True) # Attempt to reconnect. This setting is persistent.
except self.module.OperationalError, e:
raise OperationalError(
"%s; used connection string: host=%s, port=%s, db=%s, user=%s, pwd=%s" % (
- e, self.host, self.port, self.db, self.user, self.password)
+ e, self.host, self.port, self.db, self.user, self.password)
)
if hasattr(conn, 'autocommit'):
@@ -85,12 +85,12 @@
# SERVER_GONE exception, the second attempt the SERVER_LOST exception
# and only the third succeeds. Thus the 3 in the loop count.
# If it doesn't reconnect even after 3 attempts, while the database is
- # up and running, it is because a 5.0.x (or newer) server is used
- # which no longer permits autoreconnects by default. In their case a
+ # up and running, it is because a 5.0.3 (or newer) server is used
+ # which no longer permits autoreconnects by default. In that case a
# reconnect flag must be set when making the connection to indicate
- # that autoreconnecting is desired and the python-mysqldb module
- # doesn't set this flag.
- for c in range(0, 3):
+ # that autoreconnecting is desired. In MySQLdb 1.2.2 or newer this is
+ # done by calling ping(True) on the connection.
+ for count in range(3):
try:
if self.need_unicode:
# For MysqlDB 1.2.1 and later, we go
@@ -100,8 +100,8 @@
else:
return DBAPI._executeRetry(self, conn, cursor, query)
except MySQLdb.OperationalError, e:
- if e.args[0] in (2006, 2013): # SERVER_GONE or SERVER_LOST error
- if c == 2:
+ if e.args[0] in (MySQLdb.constants.CR.SERVER_GONE_ERROR, MySQLdb.constants.CR.SERVER_LOST):
+ if count == 2:
raise OperationalError(ErrorMessage(e))
if self.debug:
self.printDebug(conn, str(e), 'ERROR')
@@ -109,7 +109,7 @@
raise OperationalError(ErrorMessage(e))
except MySQLdb.IntegrityError, e:
msg = ErrorMessage(e)
- if e.args[0] == 1062:
+ if e.args[0] == MySQLdb.constants.ER.DUP_ENTRY:
raise DuplicateEntryError(msg)
else:
raise IntegrityError(msg)
|