from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker


session = None


def init_session(connection_string=None, drop=False):
    if connection_string is None:
        connection_string = 'sqlite://'

    from database.model import Base

    global session

    if drop:
        try:
            old_session = session
            Base.metadata.drop_all(bind=old_session.bind)
        except:
            pass

    engine = create_engine(connection_string, echo=True)
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    Base.metadata.create_all(bind=engine)

    session = db_session


from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.pool import StaticPool


session = None


def init_session(connection_string=None, drop=False):
    if connection_string is None:
        engine =  create_engine('sqlite://',
                                echo=True,
                                connect_args={'check_same_thread':False},
                                poolclass=StaticPool)
    else:
        engine = create_engine(connection_string)

    from database.model import Base

    global session

    if drop:
        try:
            old_session = session
            Base.metadata.drop_all(bind=old_session.bind)
        except:
            pass

    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    Base.metadata.create_all(bind=engine)

    session = db_session


Fix the database session init to work with the flask debug server.
Fix the database session init to work with the flask debug server.

The debug webserver consists of two parts: the watcher that watches
the files for changes and the worker that is forked and will be restarted
after each modification. Sqlachemy uses a SingletonPool that will not
work with this if the database was initialized within the watcher.

See [1] for more detailed information.

[1] http://docs.sqlalchemy.org/en/rel_0_8/dialects/sqlite.html#threading-pooling-behavior

  from sqlalchemy import create_engine
  from sqlalchemy.orm import scoped_session, sessionmaker
+ from sqlalchemy.pool import StaticPool
  
  
  session = None
  
  
  def init_session(connection_string=None, drop=False):
      if connection_string is None:
-         connection_string = 'sqlite://'
+         engine =  create_engine('sqlite://',
+                                 echo=True,
+                                 connect_args={'check_same_thread':False},
+                                 poolclass=StaticPool)
+     else:
+         engine = create_engine(connection_string)
  
      from database.model import Base
  
      global session
  
      if drop:
          try:
              old_session = session
              Base.metadata.drop_all(bind=old_session.bind)
          except:
              pass
  
-     engine = create_engine(connection_string, echo=True)
      db_session = scoped_session(sessionmaker(autocommit=False,
                                               autoflush=False,
                                               bind=engine))
      Base.metadata.create_all(bind=engine)
  
      session = db_session
  
  
Fix the database session init to work with the flask debug server.
## Code Before:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker


session = None


def init_session(connection_string=None, drop=False):
    if connection_string is None:
        connection_string = 'sqlite://'

    from database.model import Base

    global session

    if drop:
        try:
            old_session = session
            Base.metadata.drop_all(bind=old_session.bind)
        except:
            pass

    engine = create_engine(connection_string, echo=True)
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    Base.metadata.create_all(bind=engine)

    session = db_session


## Instruction:
Fix the database session init to work with the flask debug server.
## Code After:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.pool import StaticPool


session = None


def init_session(connection_string=None, drop=False):
    if connection_string is None:
        engine =  create_engine('sqlite://',
                                echo=True,
                                connect_args={'check_same_thread':False},
                                poolclass=StaticPool)
    else:
        engine = create_engine(connection_string)

    from database.model import Base

    global session

    if drop:
        try:
            old_session = session
            Base.metadata.drop_all(bind=old_session.bind)
        except:
            pass

    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    Base.metadata.create_all(bind=engine)

    session = db_session

