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

  • SEARCH
  • Home
  • 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 8768561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:06:01+00:00 2026-06-13T17:06:01+00:00

I figured I would make this a different question for the sake of being

  • 0

I figured I would make this a different question for the sake of being tidy. It is based on:
SQLAlchemy won't update my database and SQLAlchemy session: how to keep it alive?.

So here’s the deal: I have a Pyramid application that’s talking to a daemon, which in turn talks to a database.

Now for some reason stuff isn’t getting committed to the database when I add it to the database session variable, as in:

DBSession.add(ModelInstance)

Calling flush or commit doesn’t make it commit.

This is how I make DBSession:

    settings = {
        'sqlalchemy.url':'blah blah'
        }
    DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

This seems fine to me because I can query the database fine. ie: this sort of thing works:

DBSession.query(ModelClass).get(id)

This fine gentleman https://stackoverflow.com/users/100297/martijn-pieters suggested the use of the following little bit of code:

import transaction
transaction.commit()

And that worked fine for making sure that my stuff got committed. The only problem is that it somehow renders my DBSession useless. So if I want to use the objects that my session is keeping track of I need to re instantiate the session and those items. This sucks. It takes up a whole lot of time.

My question is, in short, how can I avoid this?

And in long:

  • How do I get my DBSession to commit properly without breaking it?

OR

  • How do I fix my DBSession and associated model instances without the need for lengthly database calls?

AND

  • Any idea why this is happening? I have successfully constructed a DBSession in the same way inside the Pyramid app I mentioned and it worked totally fine, it committed when I wanted it to and everything.

For details of the errors I encountered please refer to the two questions I mentioned in the beginning

  • 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-13T17:06:02+00:00Added an answer on June 13, 2026 at 5:06 pm

    In SQLAlchemy sessions expire the objects they are managing upon commit. This is sane because after commit you have no guarantees in a concurrent world that something else isn’t changing the state they are attempting to mirror in the database.

    Pyramid recommends the use of a transaction manager that helps you maintain a single transaction per request. It will automatically call transaction.commit() for you after the request is complete. In this way, you don’t have to think/worry about objects expiring, and transactions are properly aborted if your code raises an exception.

    The way to setup the transaction manager is by installing pyramid_tm and zope.sqlalchemy and then connecting your DBSession to zope.sqlalchemy.ZopeTransactionExtension. Then things will “just work”.

    DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    
    # ...
    
    def main(global_conf, **settings):
        config = Configurator(...)
        config.include('pyramid_tm')
        # ...
    

    If you need to populate a new object’s primary key or ensure that some SQL will execute properly you can use DBSession.flush() to execute the SQL within your transaction without actually committing it. Any errors will be raised there for you to catch and deal with.

    This basic setup of your sessions is described within the tutorial in the Pyramid documentation:

    http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/tutorials/wiki2/basiclayout.html

    Update: I realized I kind of answered your question about using the transaction manager in Pyramid, which you are already using successfully. I think that the answer also clearly explains what’s going on with the ZopeTransactionExtension, however, and you just need confirmation about committing transactions. You’d be wise to simply use one transaction in your script, which you can create via

    import transaction
    
    with transaction.manager:
        # do tons of database stuff
    

    Now if an exception happens the transaction will be aborted, and if not it will be committed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I figured this would be simple, but being new to SSRS I'm not sure.
Question: I need to make some system calls in my C# applications. Unfortunately, this
I know this question has been asked, how to make a site like facebook/gmail
so i figured i would start small... no go. the jquery UI slider does
I have 2 questions, so I figured I would cram them into 1 single
Hi I am learning Ajax + MVC. I figured it would be nice for
What is returned if $('#id') doesn't match anything? I figured it would be null
I would like to make a layout using Java Swing which looks like the
This isn't a classic programming language question, but it's keeping me busy so I
Sorry if this sounds like a bit of a noobie question, i'm still relatively

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.