Python sqlalchemy.exc.DisconnectionError() Examples
The following are 11
code examples of sqlalchemy.exc.DisconnectionError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy.exc
, or try the search function
.
Example #1
Source File: session.py From rucio with Apache License 2.0 | 6 votes |
def mysql_ping_listener(dbapi_conn, connection_rec, connection_proxy): """ Ensures that MySQL connections checked out of the pool are alive. Borrowed from: http://groups.google.com/group/sqlalchemy/msg/a4ce563d802c929f :param dbapi_conn: DBAPI connection :param connection_rec: connection record :param connection_proxy: connection proxy """ try: dbapi_conn.cursor().execute('select 1') except dbapi_conn.OperationalError as ex: if ex.args[0] in (2006, 2013, 2014, 2045, 2055): msg = 'Got mysql server has gone away: %s' % ex raise DisconnectionError(msg) else: raise
Example #2
Source File: impl_sqlalchemy.py From taskflow with Apache License 2.0 | 6 votes |
def _ping_listener(dbapi_conn, connection_rec, connection_proxy): """Ensures that MySQL connections checked out of the pool are alive. Modified + borrowed from: http://bit.ly/14BYaW6. """ try: dbapi_conn.cursor().execute('select 1') except dbapi_conn.OperationalError as ex: if _in_any(six.text_type(ex.args[0]), MY_SQL_GONE_WAY_AWAY_ERRORS): LOG.warning('Got mysql server has gone away', exc_info=True) raise sa_exc.DisconnectionError("Database server went away") elif _in_any(six.text_type(ex.args[0]), POSTGRES_GONE_WAY_AWAY_ERRORS): LOG.warning('Got postgres server has gone away', exc_info=True) raise sa_exc.DisconnectionError("Database server went away") else: raise
Example #3
Source File: db.py From DLRN with Apache License 2.0 | 5 votes |
def checkout(dbapi_connection, connection_record, connection_proxy): pid = os.getpid() if connection_record.info['pid'] != pid: connection_record.connection = connection_proxy.connection = None raise exc.DisconnectionError( "Connection record belongs to pid %s, " "attempting to check out in pid %s" % (connection_record.info['pid'], pid) )
Example #4
Source File: app.py From depsy with MIT License | 5 votes |
def ping_connection(dbapi_connection, connection_record, connection_proxy): cursor = dbapi_connection.cursor() try: cursor.execute("SELECT 1") except: # optional - dispose the whole pool # instead of invalidating one at a time # connection_proxy._pool.dispose() # raise DisconnectionError - pool will try # connecting again up to three times before raising. raise exc.DisconnectionError() cursor.close()
Example #5
Source File: impl_sqlalchemy.py From taskflow with Apache License 2.0 | 5 votes |
def validate(self, max_retries=0): """Performs basic **connection** validation of a sqlalchemy engine.""" def _retry_on_exception(exc): LOG.warning("Engine connection (validate) failed due to '%s'", exc) if isinstance(exc, sa_exc.OperationalError) and \ _is_db_connection_error(six.text_type(exc.args[0])): # We may be able to fix this by retrying... return True if isinstance(exc, (sa_exc.TimeoutError, sa_exc.ResourceClosedError, sa_exc.DisconnectionError)): # We may be able to fix this by retrying... return True # Other failures we likely can't fix by retrying... return False @tenacity.retry( stop=tenacity.stop_after_attempt(max(0, int(max_retries))), wait=tenacity.wait_exponential(), reraise=True, retry=tenacity.retry_if_exception(_retry_on_exception) ) def _try_connect(engine): # See if we can make a connection happen. # # NOTE(harlowja): note that even though we are connecting # once it does not mean that we will be able to connect in # the future, so this is more of a sanity test and is not # complete connection insurance. with contextlib.closing(engine.connect()): pass _try_connect(self._engine)
Example #6
Source File: engines.py From oslo.db with Apache License 2.0 | 5 votes |
def _add_process_guards(engine): """Add multiprocessing guards. Forces a connection to be reconnected if it is detected as having been shared to a sub-process. """ @sqlalchemy.event.listens_for(engine, "connect") def connect(dbapi_connection, connection_record): connection_record.info['pid'] = os.getpid() @sqlalchemy.event.listens_for(engine, "checkout") def checkout(dbapi_connection, connection_record, connection_proxy): pid = os.getpid() if connection_record.info['pid'] != pid: LOG.debug( "Parent process %(orig)s forked (%(newproc)s) with an open " "database connection, " "which is being discarded and recreated.", {"newproc": pid, "orig": connection_record.info['pid']}) connection_record.connection = connection_proxy.connection = None raise exc.DisconnectionError( "Connection record belongs to pid %s, " "attempting to check out in pid %s" % (connection_record.info['pid'], pid) )
Example #7
Source File: db.py From seafobj with Apache License 2.0 | 5 votes |
def ping_connection(dbapi_connection, connection_record, connection_proxy): # pylint: disable=unused-argument cursor = dbapi_connection.cursor() try: cursor.execute("SELECT 1") cursor.close() except: logging.info('fail to ping database server, disposing all cached connections') connection_proxy._pool.dispose() # pylint: disable=protected-access # Raise DisconnectionError so the pool would create a new connection raise DisconnectionError()
Example #8
Source File: app.py From impactstory-tng with MIT License | 5 votes |
def ping_connection(dbapi_connection, connection_record, connection_proxy): cursor = dbapi_connection.cursor() try: cursor.execute("SELECT 1") except: # optional - dispose the whole pool # instead of invalidating one at a time # connection_proxy._pool.dispose() # raise DisconnectionError - pool will try # connecting again up to three times before raising. raise exc.DisconnectionError() cursor.close()
Example #9
Source File: db.py From build-relengapi with Mozilla Public License 2.0 | 5 votes |
def ping_connection(dbapi_connection, connection_record, connection_proxy): cursor = dbapi_connection.cursor() try: cursor.execute("SELECT 1") except Exception: # pragma: no cover # optional - dispose the whole pool # instead of invalidating one at a time connection_proxy._pool.dispose() # raise DisconnectionError - pool will try # connecting again up to three times before raising. raise exc.DisconnectionError() cursor.close()
Example #10
Source File: __init__.py From hoaxy-backend with GNU General Public License v3.0 | 5 votes |
def checkout(dbapi_connection, connection_record, connection_proxy): pid = os.getpid() if connection_record.info['pid'] != pid: connection_record.connection = connection_proxy.connection = None raise exc.DisconnectionError("Connection record belongs to pid %s, " "attempting to check out in pid %s" % (connection_record.info['pid'], pid))
Example #11
Source File: orm_event_handlers.py From airflow with Apache License 2.0 | 4 votes |
def setup_event_handlers(engine): """ Setups event handlers. """ # pylint: disable=unused-argument, unused-variable @event.listens_for(engine, "connect") def connect(dbapi_connection, connection_record): connection_record.info['pid'] = os.getpid() if engine.dialect.name == "sqlite": @event.listens_for(engine, "connect") def set_sqlite_pragma(dbapi_connection, connection_record): cursor = dbapi_connection.cursor() cursor.execute("PRAGMA foreign_keys=ON") cursor.close() # this ensures sanity in mysql when storing datetimes (not required for postgres) if engine.dialect.name == "mysql": @event.listens_for(engine, "connect") def set_mysql_timezone(dbapi_connection, connection_record): cursor = dbapi_connection.cursor() cursor.execute("SET time_zone = '+00:00'") cursor.close() @event.listens_for(engine, "checkout") def checkout(dbapi_connection, connection_record, connection_proxy): pid = os.getpid() if connection_record.info['pid'] != pid: connection_record.connection = connection_proxy.connection = None raise exc.DisconnectionError( "Connection record belongs to pid {}, " "attempting to check out in pid {}".format(connection_record.info['pid'], pid) ) if conf.getboolean('debug', 'sqlalchemy_stats', fallback=False): @event.listens_for(engine, "before_cursor_execute") def before_cursor_execute(conn, cursor, statement, parameters, context, executemany): conn.info.setdefault('query_start_time', []).append(time.time()) @event.listens_for(engine, "after_cursor_execute") def after_cursor_execute(conn, cursor, statement, parameters, context, executemany): total = time.time() - conn.info['query_start_time'].pop() file_name = [ f"'{f.name}':{f.filename}:{f.lineno}" for f in traceback.extract_stack() if 'sqlalchemy' not in f.filename][-1] stack = [f for f in traceback.extract_stack() if 'sqlalchemy' not in f.filename] stack_info = ">".join([f"{f.filename.rpartition('/')[-1]}:{f.name}" for f in stack][-3:]) conn.info.setdefault('query_start_time', []).append(time.monotonic()) log.info("@SQLALCHEMY %s |$ %s |$ %s |$ %s ", total, file_name, stack_info, statement.replace("\n", " ") ) # pylint: enable=unused-argument, unused-variable