# All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import sys from oslo_config import cfg from oslo_db.sqlalchemy import enginefacade from oslo_log import log CONF = cfg.CONF LOG = log.getLogger(__name__) main_context_manager = enginefacade.transaction_context() api_context_manager = enginefacade.transaction_context() def _get_db_conf(conf_group, connection=None): kw = dict( connection=connection or conf_group.connection, slave_connection=conf_group.slave_connection, sqlite_fk=False, __autocommit=True, expire_on_commit=False, mysql_sql_mode=conf_group.mysql_sql_mode, connection_recycle_time=conf_group.connection_recycle_time, connection_debug=conf_group.connection_debug, max_pool_size=conf_group.max_pool_size, max_overflow=conf_group.max_overflow, pool_timeout=conf_group.pool_timeout, sqlite_synchronous=conf_group.sqlite_synchronous, connection_trace=conf_group.connection_trace, max_retries=conf_group.max_retries, retry_interval=conf_group.retry_interval) return kw def get_backend(): return sys.modules[__name__] def create_context_manager(connection=None): """Create a database context manager object. : param connection: The database connection string """ ctxt_mgr = enginefacade.transaction_context() ctxt_mgr.configure(**_get_db_conf(CONF.database, connection=connection)) return ctxt_mgr def _context_manager_from_context(context): if context: try: return context.db_connection except AttributeError: pass def get_context_manager(context): """Get a database context manager object. :param context: The request context that can contain a context manager """ return _context_manager_from_context(context) or main_context_manager def get_engine(use_slave=False, context=None): """Get a database engine object. :param use_slave: Whether to use the slave connection :param context: The request context that can contain a context manager """ ctxt_mgr = get_context_manager(context) return ctxt_mgr.get_legacy_facade().get_engine(use_slave=use_slave) def get_api_engine(): return api_context_manager.get_legacy_facade().get_engine()