Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:33:27+00:00 2026-06-12T02:33:27+00:00

I have a problem where I insert a database item using a SQLAlchemy /

  • 0

I have a problem where I insert a database item using a SQLAlchemy / Tastypie REST interface, but the item is missing when subsequently get the list of items. It shows up only after I get the list of items a second time.

I am using SQLAlchemy with Tastypie/Django running on Apache via mod_wsgi. I use a singleton Database Manager class to hold my engine and declarative_base, and with Tastypie, a separate class to get the session and make sure I roll-back if there is a problem with the commit. As in the update below, the problem occurs when I don’t close my session after inserting. Why is this necessary?

My original code was like this:

Session = scoped_session(sessionmaker(autoflush=True))

# Singleton Database Manager class for managing session
class DatabaseManager():
    engine = None
    base = None

    def ready(self):
       host='mysql+mysqldb://etc...'
       if self.engine and self.base:
            return True
        else:
            try:
                self.engine = create_engine(host, pool_recycle=3600)
                self.base = declarative_base(bind=self.engine)
                return True
            except:
                return False

    def getSession(self):
        if self.ready():
            session = Session()
            session.configure(bind=self.engine)
            return session
        else:
            return None

DM = DatabaseManager()

# A session class I use with Tastypie to ensure the session is destroyed at the
# end of the transaction, because Tastypie creates singleton Resources used for
# all threads
class MySession:
    def __init__(self):
        self.s = DM.getSession()

    def safeCommit(self):
        try:
            self.s.commit()
        except:
            self.s.rollback()
            raise

    def __del__(self):
        try:
            self.s.commit()
        except:
            self.s.rollback()
            raise


# ... Then ... when I get requests through Apache/mod_wsgi/Django/Tastypie
# First Request
obj_create():
    db = MySession()
    print db.s.query(DBClass).count() # returns 4
    newItem = DBClass()
    db.s.add(newItem)
    db.s.safeCommit()
    print db.s.query(DBClass).count() # returns 5


# Second Request after First Request returns
obj_get_list():
    db = MySession()
    print db.s.query(DBClass).count() # returns 4 ... should be 5


# Third Request is okay
obj_get_list():
    db = MySession()
    print db.s.query(DBClass).count() # returns 5

UPDATE

After further digging, it appears that the problem is my session needed to be closed after creating. Perhaps because Tastypie’s object_create() adds the SQLAlchemy object to it’s bundle, and I don’t know what happens after it leaves the function’s scope:

obj_create():
    db = MySession()
    newItem = DBClass()
    db.s.add(newItem)
    db.s.safeCommit()
    copiedObj = copyObj(newItem) # copy SQLAlchemy record into non-sa object (see below)
    db.s.close()
    return copiedObj

If someone cares to explain this in an answer, I can close the question. Also, for those who are curious, I copy my object out of SQLAlchemy like this:

class Struct:
    def __init__(self, **entries):
        self.__dict__.update(entries)

class MyTastypieResource(Resource):
    ...
    def copyObject(self, object):
        base = {}
        # self._meta is part of my tastypie resource
        for p in class_mapper(self._meta.object_class).iterate_properties:
            if p.key not in base and p.key not in self._meta.excludes:
                base[p.key] = getattr(object,p.key)
        return Struct(**base)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:33:28+00:00Added an answer on June 12, 2026 at 2:33 am

    The problem was resolved by closing my session. The update in the answer didn’t solve the problem fully – I ended up adding a middleware class to close the session at the end of a transaction. This ensured everything was written to the database. The middleware looks a bit like this:

    class SQLAlchemySessionMiddleWare(object):
        def process_response(self, request, response):
            try:
                session = MyDatabaseManger.getSession()
                session.commit()
                session.close()
            except Exception, err:
                pass
            return response
    
        def process_exception(self, request, exception):
            try:
                session = MyDatabaseManger.getSession()
                session.rollback()
                session.close()
            except Exception, err:
                pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have made a few multi activity database applications using sqlite, but i get
I am creating an insert process in php but I have a problem in
i have a proble, when insert something in foreign language into database. i have
I have a problem running an insert query via C# OleDbCommand to an Access
I have a little problem to insert from 2 different table's in C# to
I have a problem reading a txt file to insert in the mysql db
I have some problem whith such mysql_query INSERT INTO table VALUES ('', CURDATE()-1) why
I have a little problem where I would like to insert a svn diff
Gday All, I have a baffling problem whilst trying to insert some chinese characters
I'm using devexpress and i have two problems. My insert stored procedure work fine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.