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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:25:06+00:00 2026-05-23T21:25:06+00:00

I have some (php) code that performs critical updates on an InnoDB table. I’m

  • 0

I have some (php) code that performs critical updates on an InnoDB table.

I’m using (SELECT … FOR UPDATE), so I need to be inside a transaction.

This code is being executed when data is posted from a form, and I have a message system in place so that error/success messages are stored in a database, and extracted from the database next time a page is rendered.

Some pseudocode:
(I’m ommitting stuff like the try/catch blocks and escaping that I have in my real code)

beginTransaction();

query("SELECT * FROM `table` WHERE id=1 FOR UPDATE");

$x=$_POST[$x];
query("UPDATE `table` SET `field` = $X");

//add other data in the db related to $X
query("INSERT INTO `othertable` (x,y,x) VALUES (......)");


//check for various errors...
$erros=0

if ($error_condition_1) {
    messageSystem("Error Condition 1!")
    $errors+=1;
}
if ($error_condition_2) {
    messageSystem("Error Condition 2!")
    $errors+=1;
}

if ($errors) {
    rollBackTransaction();
} else {
    commitTransaction();
}

The problem should be obvious: when messageSystem stores the error in the database the changes will be rolled back later, and the user will never see the error messages.

There are two easy solutions I can see:

  1. Modify my code so that messageSystem is called outside the transaction. However it is more readable and quicker to write if I can do it inline like the above. Also, what if this is library code that may already be inside a nested transaction?
  2. Modify messageSystem so that it uses its own connection to the database. But then what if I make messageSystem more complicated, and decide that it needs to lock an entry in the session table before it adds messages to that session. This could lead to a deadlock if my main code has also locked the session for some reason.
  3. Check for errors before the transaction. But in actual fact I need to have locked the relevant rows by selecting ‘FOR UPDATE’ before I can validate the input.

And so my question: Is there any way to commit X when inside a (possibly nested) transaction such that if the transaction is rolled back, X will be committed anyway.

Alternatively is there any good way of avoiding the problem I’ve described.

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-23T21:25:06+00:00Added an answer on May 23, 2026 at 9:25 pm

    The simple answer is to use a second database connection for your error logging.

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

Sidebar

Related Questions

I have some PHP code that inserts a date into a table: INSERT INTO
I have some code on my PHP powered site that creates a random hash
I have some jQuery code. I have called an Ajax function file, file.php, that
I have a form on my PHP page which performs some ajax validation (that's
I have inherited some legacy PHP code what was written back when it was
I have some code I've written in PHP for consuming our simple webservice, which
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Hi have some forms that I want to use some basic php validation (regular
I have some old apps written in PHP that I'm thinking of converting to
I am a web-developer working in PHP. I have some limited experience with using

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.