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

import threading

# Temporary logging
"""
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
"""


threadLocal = threading.local()

class Database:
    def __init__(self, config):
        self.config = config


    def Session(self):
        engine = getattr(threadLocal, "engine", None)
        if engine is None:
            threadLocal.engine = create_engine(self.config["database_url"])

        session = getattr(threadLocal, "session", None)
        if session is None:
            session_factory = sessionmaker(bind=threadLocal.engine)
            threadLocal.session = scoped_session(session_factory)

        return threadLocal.session()


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

import threading

# Temporary logging
"""
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
"""


threadLocal = threading.local()

class Database:
    def __init__(self, config):
        self.config = config


    def Session(self):
        engine = getattr(threadLocal, "engine", None)
        if engine is None:
            threadLocal.engine = create_engine(self.config["database_url"])

        session_factory = getattr(threadLocal, "session_factory", None)
        if session_factory is None:
            session_factory = sessionmaker(bind=threadLocal.engine)
            threadLocal.session_factory = session_factory

        return session_factory()

Put the session factory in threadLocal, not the session
Put the session factory in threadLocal, not the session

  
  from sqlalchemy import create_engine
  from sqlalchemy.orm import sessionmaker, scoped_session
  
  import threading
  
  # Temporary logging
  """
  import logging
  logging.basicConfig()
  logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
  """
  
  
  threadLocal = threading.local()
  
  class Database:
      def __init__(self, config):
          self.config = config
  
  
      def Session(self):
          engine = getattr(threadLocal, "engine", None)
          if engine is None:
              threadLocal.engine = create_engine(self.config["database_url"])
  
-         session = getattr(threadLocal, "session", None)
+         session_factory = getattr(threadLocal, "session_factory", None)
-         if session is None:
+         if session_factory is None:
              session_factory = sessionmaker(bind=threadLocal.engine)
-             threadLocal.session = scoped_session(session_factory)
+             threadLocal.session_factory = session_factory
  
-         return threadLocal.session()
+         return session_factory()
  
Put the session factory in threadLocal, not the session
## Code Before:

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

import threading

# Temporary logging
"""
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
"""


threadLocal = threading.local()

class Database:
    def __init__(self, config):
        self.config = config


    def Session(self):
        engine = getattr(threadLocal, "engine", None)
        if engine is None:
            threadLocal.engine = create_engine(self.config["database_url"])

        session = getattr(threadLocal, "session", None)
        if session is None:
            session_factory = sessionmaker(bind=threadLocal.engine)
            threadLocal.session = scoped_session(session_factory)

        return threadLocal.session()

## Instruction:
Put the session factory in threadLocal, not the session
## Code After:

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

import threading

# Temporary logging
"""
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
"""


threadLocal = threading.local()

class Database:
    def __init__(self, config):
        self.config = config


    def Session(self):
        engine = getattr(threadLocal, "engine", None)
        if engine is None:
            threadLocal.engine = create_engine(self.config["database_url"])

        session_factory = getattr(threadLocal, "session_factory", None)
        if session_factory is None:
            session_factory = sessionmaker(bind=threadLocal.engine)
            threadLocal.session_factory = session_factory

        return session_factory()
