If it is an engine, then create a session from it. And indicate that
    this session should be closed after the job done.

    :type engine_or_session: Union[Engine, Session]
    :rtype: Tuple[Session, bool]

def ensure_session(engine_or_session):
    """
    If it is an engine, then create a session from it. And indicate that
    this session should be closed after the job done.

    :type engine_or_session: Union[Engine, Session]
    :rtype: Tuple[Session, bool]
    """
    if isinstance(engine_or_session, Engine):
        engine_id = id(engine_or_session)
        if id(engine_id) in session_klass_cache: # pragma: no cover
            SessionClass = session_klass_cache[engine_id]
        else:  # pragma: no cover
            SessionClass = sessionmaker(bind=engine_or_session)
        ses = SessionClass()
        auto_close = True
        return ses, auto_close
    elif isinstance(engine_or_session, Session):
        ses = engine_or_session
        auto_close = False
        return ses, auto_close