DEV: Close transactions at the end of HTTP Requests.

Fixes #84
This commit is contained in:
Scott Sanderson
2014-10-29 16:00:41 -04:00
parent db5cf9cf99
commit 8cfbe9b38e
6 changed files with 62 additions and 13 deletions

View File

@@ -381,17 +381,20 @@ class CookieToken(Token, Base):
__tablename__ = 'cookie_tokens'
def new_session(url="sqlite:///:memory:", reset=False, **kwargs):
def new_session_factory(url="sqlite:///:memory:", reset=False, **kwargs):
"""Create a new session at url"""
if url.startswith('sqlite'):
kwargs.setdefault('connect_args', {'check_same_thread': False})
kwargs.setdefault('poolclass', StaticPool)
if url.endswith(':memory:'):
# If we're using an in-memory database, ensure that only one connection
# is ever created.
kwargs.setdefault('poolclass', StaticPool)
engine = create_engine(url, **kwargs)
Session = sessionmaker(bind=engine)
session = Session()
if reset:
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
return session
session_factory = sessionmaker(bind=engine)
return session_factory