wump committed c2ba507

"Server has gone away" protection for MySQL; port to SQLAlchemy 0.6

Comments (0)

Files changed (1)

 _sqlite_re = re.compile(r'sqlite:(?:(?://(.*?))|memory)(?:\?(.*))?$')
+class MySQLPingListener(object):
+    """
+    Ping a connection on check-out. This makes sure that it is alive before
+    we actually use it.
+    """
+    def checkout(self, dbapi_con, con_record, con_proxy):
+        """Called when connection is checked out from pool."""
+        from sqlalchemy.exc import DisconnectionError
+        from _mysql_exceptions import OperationalError
+        try:
+        except OperationalError:
+            # OperationalError: (2006, 'MySQL server has gone away')
+            raise DisconnectionError("Database server went away")
 def get_engine():
     """Return the active database engine (the database engine of the active
     Furthermore the engine is created with `convert_unicode` by default.
+    options = {'convert_unicode': True, 'echo': echo}
     # special case sqlite.  We want nicer urls for that one.
     if uri.startswith('sqlite:'):
         match = _sqlite_re.match(uri)
         # provided we set it to utf-8
         if info.drivername == 'mysql':
             info.query.setdefault('charset', 'utf8')
+            options["listeners"] = [MySQLPingListener()]
     options = {'convert_unicode': True}
     to compare the trees.
-    impl = sqlalchemy.Binary
+    impl = sqlalchemy.LargeBinary
     def process_bind_param(self, value, dialect):
         if value is None:
 del key, mod, value
 #: forward some session methods to the module as well
-for name in 'delete', 'save', 'flush', 'execute', 'begin', 'mapper', \
-            'commit', 'rollback', 'clear', 'refresh', 'expire', \
+for name in 'delete', 'add', 'add_all', 'flush', 'execute', 'begin', 'mapper', \
+            'commit', 'rollback', 'expunge_all', 'refresh', 'expire', \
     setattr(db, name, getattr(session, name))