3

I haven't too much experience with Python and sqlalchemy. I checked that similar questions asked before but still i couldn't solve my problem. I have a standalone pooling pgbouncer. And i'm trying to use sqlalchemy front of pgbouncer. for the don't leave connections open i tried to use contextmanager and with statement. I think my mistake in get_db_session() method. But still couldn't find.

this is my repository.py

import logging
import threading
from contextlib import contextmanager

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.pool import StaticPool
from sqlalchemy.pool import NullPool


@contextmanager
def get_db_session():
    try:
        engine = create_engine(
            'postgresql://superuser:@localhost:6432/testdbname', poolclass=NullPool)
        session_factory = sessionmaker(bind=engine)
        Session = scoped_session(session_factory)
        some_session = Session()
        print "got new session"
        yield some_session
        print "after yield goingt to commit"
        some_session.commit()
    except Exception as ex:
        print(ex)
        some_session.roleback()
    finally:
        some_session.expunge_all()
        some_session.close()
        print "closing"


def save(entity, _clazz=None):
    if _clazz:
        if hasattr(entity, 'id'):  # usually id is None so this method acs as normal save
            _id = entity.id
        else:
            _id = entity.name
        try:
            if _id:
                found = find(_clazz, _id)
                if found is not None:
                    if isinstance(found, list):
                        for e in found:
                            delete(e)
                    else:
                        delete(found)
        except NoResultFound:
            pass

    with get_db_session() as se:
        se.add(entity)
        se.commit()

def delete(entity):
    with get_db_session() as se:
        se.delete(entity)
        se.commit()

def find_by_element_value(_clazz, element, value):
    with get_db_session() as se:
        res = se.query(_clazz).filter(element == value).all()
        se.commit()
    return res

def get_all(_class):
    print "get_all starte"
    with get_db_session()  as se:
        print "entered with"
        res = se.query(_class).all()
        print "going to commit"
        se.commit()
    return res

and here Im using it test.py

import repositories as repository 
import model 

if __name__ == '__main__':
    a = repository.get_all(model.HomeCategory)
    a[0].slug

then i get this error . Where is my mistake i could not understand.

Traceback (most recent call last):
  File "/home/sahin/workspace/myspider/myspider/spiders/test.py", line 6, in <module>
    a[0].slug
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 237, in __get__
    return self.impl.get(instance_state(instance), dict_)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 579, in get
    value = state._load_expired(state, passive)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/state.py", line 592, in _load_expired
    self.manager.deferred_scalar_loader(self, toload)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py", line 644, in load_scalar_attributes
    (state_str(state)))
DetachedInstanceError: Instance <HomeCategory at 0x7f157008dd90> is not bound to a Session; attribute refresh operation cannot proceed

Maybe someone give a help about this. Thanks.

MrCode
  • 35
  • 1
  • 1
  • 6
  • I think i found some think.My fault is i'm trying to get out query object from with statement. Untill i get data session closed and refreshed. This one works. But only i should find a way how to get out data by methods from my repository. `with repository.get_db_session() as se: res = se.query(model.HomeCategory).all() for i in res: print i.slug` – MrCode Mar 01 '18 at 15:08

1 Answers1

15

You can use session.expire_on_commit = False so session will be available for next usage.

You can also set this parameter on scoped_session init.

For more information: Session API

Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18