Python sqlalchemy.exc 模块,OperationalError() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用sqlalchemy.exc.OperationalError()

项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:QXSConsolas    作者:qxsch    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:weibo    作者:windskyer    | 项目源码 | 文件源码
def 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
    """
    try:
        dbapi_conn.cursor().execute('select 1')
    except dbapi_conn.OperationalError, ex:
        if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
            LOG.warn('Got mysql server has gone away: %s', ex)
            raise DisconnectionError("Database server went away")
        else:
            raise
项目:pybel    作者:pybel    | 项目源码 | 文件源码
def to_database(graph, connection=None, store_parts=True):
    """Stores a graph in a database.

    :param BELGraph graph: A BEL graph
    :param connection: An RFC-1738 database connection string, a pre-built :class:`Manager`, or `None`` for
                        default connection
    :type connection: None or str or pybel.manager.Manager
    :param bool store_parts: Should the graph be stored in the edge store?
    """
    manager = Manager.ensure(connection=connection)

    try:
        manager.insert_graph(graph, store_parts=store_parts)
    except IntegrityError:
        manager.session.rollback()
        log.warning('Error storing graph - other graph with same metadata'
                    ' already present. Consider incrementing the version')
    except OperationalError:
        manager.session.rollback()
        log.exception('Error storing graph - operational exception')
    except Exception as e:
        manager.session.rollback()
        raise e
项目:webspider    作者:GuozhuHe    | 项目源码 | 文件源码
def setUp(self):
        # ???????
        try:
            self.session.execute("DROP DATABASE test_spider;")
        except OperationalError as e:
            logging.warning(e)
        self.session.execute("CREATE DATABASE test_spider CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;")
        # ??????? test_spider
        self.session.execute("USE test_spider;")
        path = os.path.dirname(__file__)
        execute_sql_file(
            file_paths=[os.path.join(path, "schema.sql"), ],
            db_session=self.session
        )
        fixture_path = os.path.join(path, 'fixture')
        # ?? fixture ?? SQL ??......
        fixture_file_paths = [os.path.join(fixture_path, file) for file in os.listdir(fixture_path)]
        execute_sql_file(
            file_paths=fixture_file_paths,
            db_session=self.session
        )
项目:Monocle    作者:Noctem    | 项目源码 | 文件源码
def update_spawns(self, initial=False):
        while True:
            try:
                await run_threaded(spawns.update)
                LOOP.create_task(run_threaded(spawns.pickle))
            except OperationalError as e:
                self.log.exception('Operational error while trying to update spawns.')
                if initial:
                    raise OperationalError('Could not update spawns, ensure your DB is set up.') from e
                await sleep(15, loop=LOOP)
            except CancelledError:
                raise
            except Exception as e:
                self.log.exception('A wild {} appeared while updating spawns!', e.__class__.__name__)
                await sleep(15, loop=LOOP)
            else:
                break
项目:dd-trace-py    作者:DataDog    | 项目源码 | 文件源码
def test_engine_execute_errors(self):
        # ensures that SQL errors are reported
        with assert_raises(OperationalError):
            with self.connection() as conn:
                conn.execute('SELECT * FROM a_wrong_table').fetchall()

        traces = self.tracer.writer.pop_traces()
        # trace composition
        eq_(len(traces), 1)
        eq_(len(traces[0]), 1)
        span = traces[0][0]
        # span fields
        eq_(span.name, '{}.query'.format(self.VENDOR))
        eq_(span.service, self.SERVICE)
        eq_(span.resource, 'SELECT * FROM a_wrong_table')
        eq_(span.get_tag('sql.db'), self.SQL_DB)
        ok_(span.get_tag('sql.rows') is None)
        eq_(span.span_type, 'sql')
        ok_(span.duration > 0)
        # check the error
        eq_(span.error, 1)
        eq_(span.get_tag('error.msg'), 'no such table: a_wrong_table')
        ok_('OperationalError' in span.get_tag('error.type'))
        ok_('OperationalError: no such table: a_wrong_table' in span.get_tag('error.stack'))
项目:chihu    作者:yelongyu    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:chihu    作者:yelongyu    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:ShelbySearch    作者:Agentscreech    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:ShelbySearch    作者:Agentscreech    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:Price-Comparator    作者:Thejas-1    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:Price-Comparator    作者:Thejas-1    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:opp    作者:openpassphrase    | 项目源码 | 文件源码
def init(config):
    db_connect = config.conf['db_connect']
    if not db_connect:
        sys.exit("Error: database connection string not "
                 "found in any of the configuration files")
    try:
        engine = create_engine(db_connect)
    except exc.NoSuchModuleError as e:
        sys.exit("Error: %s" % str(e))
    try:
        if not database_exists(engine.url):
            printv(config, "Creating database: 'openpassphrase'")
            create_database(engine.url)
    except exc.OperationalError as e:
        sys.exit("Error: %s" % str(e))
    printv(config, "Creating tables based on models")
    models.Base.metadata.create_all(engine)
项目:actsys    作者:intel-ctrlsys    | 项目源码 | 文件源码
def main():
    """Command line interface for setup database"""
    parser = ArgumentParser(description="Setup control database")
    parser.add_argument("--alembic-ini", dest='alembic_ini',
                        metavar='/path/to/alembic/config/file',
                        type=str, default="schema_migration.ini",
                        help="A path to the Alembic config file")
    args = parser.parse_args()

    save_cwd = os.getcwd()
    ini_dir, ini_file = os.path.split(args.alembic_ini)
    try:
        os.chdir(os.path.join(save_cwd, ini_dir))
        models.setup(alembic_ini=ini_file)
    except OperationalError as e:
        print(("\nException: %s\n" % e))
        print("Make sure the database exists and the current user has "
              "proper permissions to create tables and issue DDL & DML "
              "statements.")
    finally:
        os.chdir(save_cwd)
项目:python-sqlalchemy    作者:carlosalberto    | 项目源码 | 文件源码
def test_trace_text_error(self):
        tracer = DummyTracer()
        sqlalchemy_opentracing.init_tracing(tracer, False, trace_all_queries=True)
        sqlalchemy_opentracing.register_engine(self.engine)

        try:
            self.engine.execute('SELECT name FROM users')
        except OperationalError:
            pass

        self.assertEqual(1, len(tracer.spans))
        self.assertEqual(tracer.spans[0].operation_name, 'textclause')
        self.assertEqual(tracer.spans[0].is_finished, True)
        self.assertEqual(tracer.spans[0].tags, {
            'component': 'sqlalchemy',
            'db.statement': 'SELECT name FROM users',
            'db.type': 'sql',
            'sqlalchemy.dialect': 'sqlite',
            'sqlalchemy.exception': 'no such table: users',
            'error': 'true',
        })
项目:python-sqlalchemy    作者:carlosalberto    | 项目源码 | 文件源码
def test_traced_text_error(self):
        tracer = DummyTracer()
        sqlalchemy_opentracing.init_tracing(tracer, False, False)
        sqlalchemy_opentracing.register_engine(self.engine)

        session = self.session
        span = DummySpan('parent span')
        sqlalchemy_opentracing.set_parent_span(session, span)
        try:
            session.execute('SELECT zipcode FROM addresses')
        except OperationalError:
            pass

        self.assertEqual(1, len(tracer.spans))
        self.assertEqual(tracer.spans[0].operation_name, 'textclause')
        self.assertEqual(tracer.spans[0].is_finished, True)
        self.assertEqual(tracer.spans[0].child_of, span)
        self.assertEqual(tracer.spans[0].tags, {
            'component': 'sqlalchemy',
            'db.statement': 'SELECT zipcode FROM addresses',
            'db.type': 'sql',
            'sqlalchemy.dialect': 'sqlite',
            'sqlalchemy.exception': 'no such table: addresses',
            'error': 'true',
        })
项目:SuperFlaskSeed    作者:saurabh1e    | 项目源码 | 文件源码
def patch_resource(self, obj):
        if self.has_change_permission(obj) and obj:
            obj, errors = self.schema().load(request.json, instance=obj, partial=True)
            if errors:
                db.session.rollback()
                return {'error': True, 'message': str(errors)}, 400

            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                raise SQLIntegrityError(data={}, message='Integrity Error', operation='Adding Resource',
                                        status=400)
            except OperationalError:
                db.session.rollback()
                raise SQlOperationalError(data={}, message='Operational Error', operation='Adding Resource',
                                          status=400)
            return {'success': True, 'message': 'obj updated successfully',
                    'data': self.schema(exclude=tuple(self.obj_exclude), only=tuple(self.obj_only))
                            .dump(obj).data}, 200

        return {'error': True, 'message': 'Forbidden Permission Denied To Change Resource'}, 403
项目:SuperFlaskSeed    作者:saurabh1e    | 项目源码 | 文件源码
def save_resource(self):
        data = request.json if isinstance(request.json, list) else [request.json]
        objects, errors = self.schema().load(data, session=db.session, many=True)
        if errors:
            db.session.rollback()
            return {'error': True, 'message': str(errors)}, 400

        if self.has_add_permission(objects):
            db.session.add_all(objects)
        else:
            db.session.rollback()
            return {'error': True, 'message': 'Forbidden Permission Denied To Add Resource'}, 403
        try:
            db.session.commit()
        except IntegrityError as e:
            db.session.rollback()
            print(e)
            raise SQLIntegrityError(data=data, message='Integrity Error', operation='Adding Resource', status=400)
        except OperationalError:
            db.session.rollback()
            raise SQlOperationalError(data=data, message='Operational Error', operation='Adding Resource', status=400)
        return {'success': True, 'message': 'Resource added successfully',
                'data': self.schema(exclude=tuple(self.obj_exclude), only=tuple(self.obj_only))
                    .dump(objects, many=True).data}, 201
项目:SuperFlaskSeed    作者:saurabh1e    | 项目源码 | 文件源码
def add_relation(self, data):
        obj, errors = self.schema().load(data, session=db.session)
        if errors:
            raise CustomException(data=data, message=str(errors), operation='adding relation')

        if self.has_add_permission(obj, data):
            db.session.add(obj)
            try:
                db.session.commit()
            except IntegrityError as e:
                raise SQLIntegrityError(data=data, message=str(e), operation='adding relation', status=400)
            except OperationalError as e:
                raise SQLIntegrityError(data=data, message=str(e), operation='adding relation', status=400)
        else:
            raise RequestNotAllowed(data=data, message='Object not Found', operation='adding relation',
                                    status=401)
项目:SuperFlaskSeed    作者:saurabh1e    | 项目源码 | 文件源码
def update_relation(self, data):
        obj = self.model.query.get(data['id'])
        if obj:
            obj, errors = self.schema().load(data, instance=obj)
            if errors:
                raise CustomException(data=data, message=str(errors), operation='updating relation')
            if self.has_change_permission(obj, data):
                raise CustomException(data=data, message='Permission Denied', operation='adding relation')
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                raise SQLIntegrityError(data=data, message='Integrity Error', operation='Adding Resource', status=400)
            except OperationalError:
                db.session.rollback()
                raise SQlOperationalError(data=data, message='Operational Error', operation='Adding Resource',
                                          status=400)
            else:
                raise RequestNotAllowed(data=data, message='Object not Found', operation='deleting relation',
                                        status=401)
        else:
            raise ResourceNotFound(data=data, message='Object not Found', operation='Updating relation', status=404)
项目:MishMash    作者:nicfit    | 项目源码 | 文件源码
def _displayMetaInfo(self):
        display_list = DisplayList()

        def mkkey(k):
            return Style.bright(Fg.blue(str(k)))

        def mkval(v):
            return str(v)

        display_list.add(mkkey("Version"), mkval(version))
        display_list.add(mkkey("Database URL"),
                         mkval(safeDbUrl(self.config.db_url)))
        try:
            meta = self.db_session.query(Meta).one()
        except (ProgrammingError, OperationalError) as ex:
            print("\nError querying metadata. Database may not be "
                  "initialized: %s" % str(ex), file=sys.stderr)
            return 1

        display_list.add(mkkey("Database version"), mkval(meta.version))
        display_list.add(mkkey("Last sync"), mkval(meta.last_sync or "Never"))
        display_list.add(mkkey("Configuration files "),
                         mkval(", ".join(self.args.config.input_filenames)))
        display_list.print("{k} {delim} {v}", delim=Style.bright(":"))
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
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, 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
项目:webapp    作者:superchilli    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:webapp    作者:superchilli    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:netgrph    作者:yantisj    | 项目源码 | 文件源码
def authenticate_user(username, password):
    """ Authenticate a user """

    try:
        user = User.query.filter_by(username=username).first()
    except OperationalError:
        db.create_all()
        user = User.query.filter_by(username=username).first()


    authenticated = False

    if user:
        authenticated = sha256_crypt.verify(password, user.password)
    else:
        time.sleep(1)
        logger.info("Authentication Error: User not found in DB: %s", username)
        return False

    if authenticated:
        logger.debug("Successfully Authenticated user: %s", username)
    else:
        logger.info("Authentication Failed: %s", username)

    return authenticated
项目:netgrph    作者:yantisj    | 项目源码 | 文件源码
def authenticate_user(username, password):
    """ Authenticate a user """

    try:
        user = User.query.filter_by(username=username).first()
    except OperationalError:
        db.create_all()
        user = User.query.filter_by(username=username).first()


    authenticated = False

    if user:
        authenticated = sha256_crypt.verify(password, user.password)
    else:
        time.sleep(1)
        logger.info("Authentication Error: User not found in DB: %s", username)
        return False

    if authenticated:
        logger.debug("Successfully Authenticated user: %s", username)
    else:
        logger.info("Authentication Failed: %s", username)

    return authenticated
项目:QualquerMerdaAPI    作者:tiagovizoto    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:QualquerMerdaAPI    作者:tiagovizoto    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:gardenbot    作者:GoestaO    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:gardenbot    作者:GoestaO    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:flask-zhenai-mongo-echarts    作者:Fretice    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:flask-zhenai-mongo-echarts    作者:Fretice    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:open-pos-api    作者:saurabh1e    | 项目源码 | 文件源码
def patch_resource(self, obj):
        if self.has_change_permission(obj) and obj:
            obj, errors = self.schema(exclude=self.exclude_related_resource).load(request.json, instance=obj,
                                                                                  partial=True)
            if errors:
                db.session.rollback()
                return {'error': True, 'message': str(errors)}, 400

            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                raise SQLIntegrityError(data={}, message='Integrity Error', operation='Adding Resource',
                                        status=400)
            except OperationalError:
                db.session.rollback()
                raise SQlOperationalError(data={}, message='Operational Error', operation='Adding Resource',
                                          status=400)
            return {'success': True, 'message': 'obj updated successfully',
                    'data': self.schema(exclude=tuple(self.obj_exclude), only=tuple(self.obj_only))
                        .dump(obj).data}, 200

        return {'error': True, 'message': 'Forbidden Permission Denied To Change Resource'}, 403
项目:open-pos-api    作者:saurabh1e    | 项目源码 | 文件源码
def save_resource(self):
        data = request.json if isinstance(request.json, list) else [request.json]
        objects, errors = self.schema().load(data, session=db.session, many=True)
        if errors:
            db.session.rollback()
            return {'error': True, 'message': str(errors)}, 400

        if self.has_add_permission(objects):
            db.session.add_all(objects)
        else:
            db.session.rollback()
            return {'error': True, 'message': 'Forbidden Permission Denied To Add Resource'}, 403
        try:
            db.session.commit()
        except IntegrityError as e:
            db.session.rollback()
            print(e)
            raise SQLIntegrityError(data=data, message='Integrity Error', operation='Adding Resource', status=400)
        except OperationalError:
            db.session.rollback()
            raise SQlOperationalError(data=data, message='Operational Error', operation='Adding Resource', status=400)
        return {'success': True, 'message': 'Resource added successfully',
                'data': self.schema(exclude=tuple(self.obj_exclude), only=tuple(self.obj_only))
                    .dump(objects, many=True).data}, 201
项目:open-pos-api    作者:saurabh1e    | 项目源码 | 文件源码
def add_relation(self, data):
        obj, errors = self.schema().load(data, session=db.session)
        if errors:
            raise CustomException(data=data, message=str(errors), operation='adding relation')

        if self.has_add_permission(obj, data):
            db.session.add(obj)
            try:
                db.session.commit()
            except IntegrityError as e:
                raise SQLIntegrityError(data=data, message=str(e), operation='adding relation', status=400)
            except OperationalError as e:
                raise SQLIntegrityError(data=data, message=str(e), operation='adding relation', status=400)
        else:
            raise RequestNotAllowed(data=data, message='Object not Found', operation='adding relation',
                                    status=401)
项目:open-pos-api    作者:saurabh1e    | 项目源码 | 文件源码
def update_relation(self, data):
        obj = self.model.query.get(data['id'])
        if obj:
            obj, errors = self.schema().load(data, instance=obj)
            if errors:
                raise CustomException(data=data, message=str(errors), operation='updating relation')
            if self.has_change_permission(obj, data):
                raise CustomException(data=data, message='Permission Denied', operation='adding relation')
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                raise SQLIntegrityError(data=data, message='Integrity Error', operation='Adding Resource', status=400)
            except OperationalError:
                db.session.rollback()
                raise SQlOperationalError(data=data, message='Operational Error', operation='Adding Resource',
                                          status=400)
            else:
                raise RequestNotAllowed(data=data, message='Object not Found', operation='deleting relation',
                                        status=401)
        else:
            raise ResourceNotFound(data=data, message='Object not Found', operation='Updating relation', status=404)
项目:Data-visualization    作者:insta-code1    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:simphony-remote    作者:simphony    | 项目源码 | 文件源码
def __init__(self, url, **kwargs):
        """Initialises a database connection to a given database url.

        Parameters
        ----------
        url : url
            A sqlalchemy url to connect to a specified database.
        kwargs : dict
            Additional keys will be passed at create_engine.
        """
        super().__init__()

        self.url = url

        self.log.info("Creating session to db: {}".format(self.url))
        self.engine = create_engine(self.url, **kwargs)

        try:
            self.session_class = sessionmaker(bind=self.engine)
        except OperationalError:
            self.log.exception(
                "Failed to connect db session: {}".format(self.url)
            )
            raise
项目:carsus    作者:tardis-sn    | 项目源码 | 文件源码
def chianti_ions_table(self):

        if self._chianti_ions_table is None:

            chianti_ions_table_name = "ChiantiIonList" + str(hash(frozenset(self.chianti_ions)))

            try:
                chianti_ions_table = Base.metadata.tables[convert_camel2snake(chianti_ions_table_name)]
            except KeyError:
                chianti_ions_table = type(chianti_ions_table_name, (Base, IonListMixin), dict()).__table__

            try:
                # To create the temporary table use the session's current transaction-bound connection
                chianti_ions_table.create(self.session.connection())
            except OperationalError:  # Raised if the table already exists
                pass
            else:
                # Insert values from `ions` into the table
                if self.chianti_ions:
                    self.session.execute(chianti_ions_table.insert(),
                                         [{"atomic_number": atomic_number, "ion_charge": ion_charge}
                                          for atomic_number, ion_charge in self.chianti_ions])

            self._chianti_ions_table = chianti_ions_table
        return self._chianti_ions_table
项目:micro-blog    作者:nickChenyx    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:datapackage-pipelines    作者:frictionlessdata    | 项目源码 | 文件源码
def initialize(self, parameters):
        super(SQLDumper, self).initialize(parameters)
        table_to_resource = parameters['tables']
        engine = parameters.get('engine', 'env://DPP_DB_ENGINE')
        if engine.startswith('env://'):
            env_var = engine[6:]
            engine = os.environ.get(env_var)
            assert engine is not None, \
                "Couldn't connect to DB - " \
                "Please set your '%s' environment variable" % env_var
        self.engine = create_engine(engine)
        try:
            self.engine.connect()
        except OperationalError:
            logging.exception('Failed to connect to database %s', engine)
            raise

        for k, v in table_to_resource.items():
            v['table-name'] = k

        self.converted_resources = \
            dict((v['resource-name'], v) for v in table_to_resource.values())

        self.updated_column = parameters.get("updated_column")
        self.updated_id_column = parameters.get("updated_id_column")
项目:python-flask-security    作者:weinbergdavid    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:watcher    作者:nosmokingbandit    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:Lixiang_zhaoxin    作者:hejaxian    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:flask    作者:bobohope    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:Chorus    作者:DonaldBough    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break
项目:Alfred    作者:jkachhadia    | 项目源码 | 文件源码
def _pg_create_db(cfg, eng, ident):
    with eng.connect().execution_options(
            isolation_level="AUTOCOMMIT") as conn:
        try:
            _pg_drop_db(cfg, conn, ident)
        except Exception:
            pass
        currentdb = conn.scalar("select current_database()")
        for attempt in range(3):
            try:
                conn.execute(
                    "CREATE DATABASE %s TEMPLATE %s" % (ident, currentdb))
            except exc.OperationalError as err:
                if attempt != 2 and "accessed by other users" in str(err):
                    time.sleep(.2)
                    continue
                else:
                    raise
            else:
                break