title: Using sqlalchemy and context manager the database is not updating
body: <p>I am trying to update a database using sqlalchemy and I need the connection/session to be closed after the execution of the script to be used by another action (delete). I rewrite a part of a previous code (not mine) and I tried both of codes below but unfortunately nothing new is written in the database. It is not updating the database with a new value if I check with Database Browser or by the code (with lookup function).</p>
  
  <p>Thank you very much by advance</p>
  
  <p>First code I tried:</p>
  
  <pre><code>import os
  import sqlalchemy as sa
  from sqlalchemy.orm import sessionmaker
  from contextlib import contextmanager
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Column, ForeignKey, Integer, String
  from sqlalchemy import create_engine
  
  import logging
  
  
  logging.basicConfig()
  logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
  
  Base = declarative_base()
  path1=os.getcwd()
  datastore_file = os.path.join(
          path1,
          '../mydatabase.db'
      )
  engine = sa.create_engine('sqlite:///{}'.format(datastore_file))
  Base.metadata.create_all(engine)
  Session = sessionmaker(bind=engine)
  
  class ComponentValue(Base):
      __tablename__ = 'component_value'
      id = Column(Integer, primary_key=True)
      value = Column(String(64), nullable=False)
  
  class ThingOne(object):
      def go(self, session,ct):
          (session.query(ComponentValue).filter(ComponentValue.value == ct.value)).first()
  def lookup(ct):
      session = Session()
      try:
          val =ThingOne().go(session,ct) 
          session.commit()
      except:
          session.rollback()
          raise
      finally:
          session.close()
      return val
  
  def update(ct):
      session = Session()
      try:
          val = ThingOne().go(session,ct)
  
          if not val and len(ct.value.strip()):
              val = ComponentValue(value=ct.value)
              session.add(val)
          session.commit()
      except:
          session.rollback()
          raise
      finally:
          session.close()
  </code></pre>
  
  <p>Second code I also tried:</p>
  
  <pre><code>import os
  import sqlalchemy as sa
  from sqlalchemy.orm import sessionmaker
  from contextlib import contextmanager
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Column, ForeignKey, Integer, String
  from sqlalchemy.orm import relationship
  from sqlalchemy import create_engine
  import logging
  
  logging.basicConfig()
  logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
  
  Base = declarative_base()
  
  path1=os.getcwd()
  datastore_file = os.path.join(
          path1,
          '../mydatabase.db'
      )
  
  engine = sa.create_engine('sqlite:///{}'.format(datastore_file))
  Base.metadata.create_all(engine)
  Session = sessionmaker()
  Session.configure(bind=engine)
  
  class ComponentValue(Base):
      __tablename__ = 'component_value'
      id = Column(Integer, primary_key=True)
      value = Column(String(64), nullable=False)
  
  @contextmanager
  def session_scope():
      session = Session()
      try:
          yield session
          session.commit()
      except:
          session.rollback()
          raise
      finally:
          session.close()
  
  class ThingOne(object):
      def go(self, session,ct):
          (session.query(ComponentValue).filter(ComponentValue.value == ct.value)).first()
  
  def lookup(ct):
      with session_scope() as session:
          val =ThingOne().go(session,ct)        
          return val
  
  def update(ct):
      with session_scope() as session:
          val = ThingOne().go(session,ct)
          if not val and len(ct.value.strip()):
              val = ComponentValue(value=ct.value)
              session.add(val)
          session.commit()
  </code></pre>