Ejemplo n.º 1
0
def create_engine(sql_connection, sqlite_fk=False, mysql_sql_mode=None,
                  mysql_enable_ndb=False,
                  connection_recycle_time=3600,
                  connection_debug=0, max_pool_size=None, max_overflow=None,
                  pool_timeout=None, sqlite_synchronous=True,
                  connection_trace=False, max_retries=10, retry_interval=10,
                  thread_checkin=True, logging_name=None,
                  json_serializer=None,
                  json_deserializer=None, connection_parameters=None):
    """Return a new SQLAlchemy engine."""

    url = sqlalchemy.engine.url.make_url(sql_connection)

    if connection_parameters:
        url = _extend_url_parameters(url, connection_parameters)

    _vet_url(url)

    engine_args = {
        "pool_recycle": connection_recycle_time,
        'connect_args': {},
        'logging_name': logging_name
    }

    _setup_logging(connection_debug)

    _init_connection_args(
        url, engine_args,
        max_pool_size=max_pool_size,
        max_overflow=max_overflow,
        pool_timeout=pool_timeout,
        json_serializer=json_serializer,
        json_deserializer=json_deserializer,
    )

    engine = sqlalchemy.create_engine(url, **engine_args)

    if mysql_enable_ndb:
        ndb.enable_ndb_support(engine)

    _init_events(
        engine,
        mysql_sql_mode=mysql_sql_mode,
        sqlite_synchronous=sqlite_synchronous,
        sqlite_fk=sqlite_fk,
        thread_checkin=thread_checkin,
        connection_trace=connection_trace
    )

    # register alternate exception handler
    exc_filters.register_engine(engine)

    # register engine connect handler
    event.listen(engine, "engine_connect", _connect_ping_listener)

    # initial connect + test
    # NOTE(viktors): the current implementation of _test_connection()
    #                does nothing, if max_retries == 0, so we can skip it
    if max_retries:
        test_conn = _test_connection(engine, max_retries, retry_interval)
        test_conn.close()

    return engine
Ejemplo n.º 2
0
def create_engine(sql_connection, sqlite_fk=False, mysql_sql_mode=None,
                  mysql_enable_ndb=False,
                  connection_recycle_time=3600,
                  connection_debug=0, max_pool_size=None, max_overflow=None,
                  pool_timeout=None, sqlite_synchronous=True,
                  connection_trace=False, max_retries=10, retry_interval=10,
                  thread_checkin=True, logging_name=None,
                  json_serializer=None,
                  json_deserializer=None, connection_parameters=None):
    """Return a new SQLAlchemy engine."""

    url = sqlalchemy.engine.url.make_url(sql_connection)

    if connection_parameters:
        _extend_url_parameters(url, connection_parameters)

    _vet_url(url)

    engine_args = {
        "pool_recycle": connection_recycle_time,
        'connect_args': {},
        'logging_name': logging_name
    }

    _setup_logging(connection_debug)

    _init_connection_args(
        url, engine_args,
        max_pool_size=max_pool_size,
        max_overflow=max_overflow,
        pool_timeout=pool_timeout,
        json_serializer=json_serializer,
        json_deserializer=json_deserializer,
    )

    engine = sqlalchemy.create_engine(url, **engine_args)

    if mysql_enable_ndb:
        ndb.enable_ndb_support(engine)

    _init_events(
        engine,
        mysql_sql_mode=mysql_sql_mode,
        sqlite_synchronous=sqlite_synchronous,
        sqlite_fk=sqlite_fk,
        thread_checkin=thread_checkin,
        connection_trace=connection_trace
    )

    # register alternate exception handler
    exc_filters.register_engine(engine)

    # register engine connect handler
    event.listen(engine, "engine_connect", _connect_ping_listener)

    # initial connect + test
    # NOTE(viktors): the current implementation of _test_connection()
    #                does nothing, if max_retries == 0, so we can skip it
    if max_retries:
        test_conn = _test_connection(engine, max_retries, retry_interval)
        test_conn.close()

    return engine