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 8626043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:55:41+00:00 2026-06-12T07:55:41+00:00

I am using adbapi in a cyclone web server. First my handler writes some

  • 0

I am using adbapi in a cyclone web server. First my handler writes some stuff to a SQL database, then it makes to an HTTP request to another web server. If that HTTP request fails, I want the database transaction to rollback. However, I’m not getting that effect. Looking at the documentation, it says

The function will be called in the thread with a
twisted.enterprise.adbapi.Transaction, which basically mimics a DB-API
cursor. In all cases a database transaction will be commited after
your database usage is finished, unless an exception is raised in
which case it will be rolled back.

This isn’t quite as precise a statement as I would like. At what point exactly is my “database usage finished”? Is that when the handler’s self.finish() method is called? When the method passed into ConnectionPool.runInteraction() is done?

Here is my code

class AccountCreationHandler(BaseRequestHandler):
    @cyclone.web.asynchronous
    def post(self, accessKey, *args, **kwargs):
        try:    
            d = connPool.runInteraction(self.saveStuffToDatabase)
            d.addCallback(self.callWebServer)
            d.addCallback(self.formatResult)
            d.addErrback(self.handleFailure)

        except Exception, e:
            self.handleException(e)


    def saveStuffToDatabase(self, txn):
        txn.execute("INSERT INTO Table1 (f1) VALUES ('v1')")


    def callWebServer(self):
        agent = Agent(reactor)
        hdrs = Headers({ "Content-type": ["application/json"] })
        values = json.dumps({ "someField": 123 })
        body = SimpleProducer(values)
        url = "http://somewebserver.com"
        d = agent.request("POST", url, hdrs, body)
        d.addCallback(self.handleWebResponse)
        return d


    def handleWebResponse(self, response):
        if response.code == 200:
            d = Deferred()
            receiver = SimpleReceiver(d)
            response.deliverBody(receiver)
            d.addCallback(self.saveWebServerResults)
            return d
        else:
            raise Exception("web server failed with http status code %d" % response.code)


    def saveWebServerResults(self, body):
        self.results = body


    def formatResult(self):    
        self.finish(self.results)


class SimpleProducer(object):
    implements(IBodyProducer)

    def __init__(self, body):
        self.body = body
        self.length = len(body)

    def startProducing(self, consumer):
        consumer.write(self.body)
        return succeed(None)

    def pauseProducing(self):
        pass

    def stopProducing(self):
        pass


class SimpleReceiver(Protocol):
    def __init__(self, d):
        self.buf = ''
        self.d = d

    def dataReceived(self, data):
        self.buf += data

    def connectionLost(self, reason):
        if type(reason.value) == ResponseDone:
            self.d.callback(self.buf)
        else:
            self.d.errback(reason)

In the case that the web server throws an error or the connection to it times out or basically if the code gets past the saveStuffToDatabase method, nothing gets rolled back when an error occurs.

I’m guessing that means that the transaction is committed when the method passed into ConnectionPool.runInteraction() has finished without throwing an exception. If that’s the case, I guess then I would have to put everything, including the call to the web server synchronously inside saveStuffToDatabase()?

  • 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-12T07:55:43+00:00Added an answer on June 12, 2026 at 7:55 am

    Well, I reimplemented the code using synchronous calls and it does appear to work correctly. Looking at the documentation for the runInteraction() method, it’s made a little more clear:

    def runInteraction(self, interaction, *args, **kw):

    Interact with the database and return the result.
    The ‘interaction’ is a callable object which will be executed in a thread using a pooled connection. It will be passed an Transaction object as an argument (whose interface is identical to that of the database cursor for your DB-API module of choice), and its results will be returned as a Deferred. If running the method raises an exception, the transaction will be rolled back. If the method returns a value, the transaction will be committed.

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

Sidebar

Related Questions

Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database
Using SQL Server 2008 R2 we are looking for a way to select the
Using Microsoft SQL Server 2005, is there any way to see when a table
Using SQL Server 2000 Table1 Id date --- ---------- 001 23/01/2012 002 25/01/2012 003
using SQL server 2005+ for over a year now, I've been running a query
I'm developing a twisted.web server - it consists of some resources that apart from
Using SQL Server 2005 Leave Table ID StartDate EndDate 001 02/03/2010 02/03/2010 002 02/03/2010
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Using WebViewBrush I can render web page content (it's screen shot) to e.g. Rectangle

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.