title: SQLAlchemy: proper way to wrap the current session into a model?
body: <p>I'm using declarative_base when constructing my models with SQLalchemy...
  so I have a model that looks like this:</p>
  
  <pre><code>from sqlalchemy.orm import sessionmaker, scoped_session                             
  engine = create_engine("mysql...")
  session_factory = sessionmaker(bind=engine)                                         
  SessionHeap = scoped_session(session_factory)          
  Base = declarative_base()
  
  class MyTable(Base, Actions):                                                      
      __tablename__ = 'mytable'                                                     
      id = Column(Integer, primary_key=True)                                       
      date = Column(Integer)                                                                                                      
      tag = Column(String(50))
  
  class Actions(object):                                                              
      @classmethod                                                                    
      def create(cls, **kwargs):                                                      
          session = SessionHeap()                                                     
          session.add(cls(**kwargs))                                                  
          session.commit()                                                            
          session.close()                                                             
          SessionHeap.remove()                                                        
  
      def update(self, **kwargs):                                                     
          for key, val in kwargs.iteritems():                                         
              self.__setattr__(key, val)                                              
          session = inspect(self).session                                             
          session.commit()                                                            
  
      @classmethod                                                                    
      def _search(cls, **kwargs):                                                     
          session = SessionHeap()                                                     
          query = session.query(cls).filter_by(**kwargs)                              
          session.commit()                                                            
          session.close()                                                             
          SessionHeap.remove()                                                        
          return query         
  </code></pre>
  
  <p>I'm not sure when I should be calling session.close() or even when to remove the session, and I've been having troubles, especially with queries, where certain sessions do not close and the database gets hung. What is a better pattern for this? (note I want to keep all methods wrapped into the model itself, not in some other global namespace)</p>