def do_begin(conn):
    """
    To allows SQLite serializable isolation, per http://docs.sqlalchemy.org/en
    /latest/dialects/sqlite.html#serializable-transaction-isolation
    """
    if SQLALCHEMY_DATABASE_URI.startswith('sqlite'):
        conn.execute("BEGIN")
"""
Creation of database, connection to database, sessions for use of database
"""
from sqlalchemy import create_engine, event
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from contextlib import contextmanager
from functools import wraps
from rvr.local_settings import SQLALCHEMY_DATABASE_URI

ENGINE = create_engine(SQLALCHEMY_DATABASE_URI, echo=False,
# Changing isolation level could cause race conditions. Be very careful.
                       isolation_level='SERIALIZABLE'
                       if SQLALCHEMY_DATABASE_URI.startswith('sqlite')
                       else 'READ COMMITTED',
                       pool_recycle=120)
SESSION = sessionmaker(bind=ENGINE)
BASE = declarative_base()

#pylint:disable=R0903

@event.listens_for(ENGINE, "begin")
def do_begin(conn):
    """
    To allows SQLite serializable isolation, per http://docs.sqlalchemy.org/en
    /latest/dialects/sqlite.html#serializable-transaction-isolation
    """
    if SQLALCHEMY_DATABASE_URI.startswith('sqlite'):
        conn.execute("BEGIN")

# from http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html