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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:47:08+00:00 2026-05-29T23:47:08+00:00

So I’ve inherited a large c++ code base which does a lot of mysql

  • 0

So I’ve inherited a large c++ code base which does a lot of mysql work. The code always disables autocommit and tends to have functions which look like this:

int function() {
    if(mysql_real_query(conn, ...)) {
        return -1;
    }

    if(mysql_real_query(conn, ...)) {
        return -1;
    }

    mysql_commit(conn);
    return 0;
}

Obviously, the intention here is that the commit only happens if the queries are successful. But what happens if one of them isn’t? Eventually, the mysql connection is properly closed, but there is no rollbacks in the code.

So when it closes, will it basically just commit any changes that were successful? Or will it rollback as if nothing happened?

My gut says that it makes sense to have a rollback if the second query fails in order to “undo” the successful first query. So this function ends up be transactional.

Of course, I find this code to be inherently broken because later other mysql code could do a commit leaving things in a “weird” state if some previous work failed. But before I go and change the behavior of the program, I wanted to make sure I understood what the current behavior was.

  • 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-05-29T23:47:10+00:00Added an answer on May 29, 2026 at 11:47 pm

    As Marc B said, this is actually easy enough to test. I was hoping for someone to be able to point at an authoritative source, but testing seems to be reasonable enough:

    So anyway, I tried the following code:

    MYSQL *conn = mysql_init(NULL);
    mysql_real_connect(conn, host, use, password, database, 0, NULL, 0);
    mysql_autocommit(conn, 0);
    mysql_query(conn, "INSERT INTO test VALUES(1)");
    mysql_query(conn, "INSERT INTO test VALUES(2)");
    mysql_query(conn, "INSERT INTO test VALUES(3)");
    mysql_query(conn, "INSERT INTO test VALUES(4)");
    mysql_close(conn);
    

    simple enough, turn off auto-commit, do a bunch of inserts, and never commit. In this case, result is that the queries are effectively rolled back. When I query the DB, the rows are not there.

    Obviously simply adding a mysql_commit(conn); right before the mysql_close(conn); does in fact cause the rows to be created.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have this code to decode numeric html entities to the UTF8 equivalent character.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.