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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:51:03+00:00 2026-05-25T23:51:03+00:00

I have a do…while loop and a try…catch block that wraps around database centric

  • 0

I have a do…while loop and a try…catch block that wraps around database centric code. The do…while and try…catch serves the purpose of preventing transaction deadlocks, and it does so very well from extensive testing. My problem now is code redundancy. I cannot figure out a way to deploy a function or method that performs the do…while and try…catch around a generic set of database centric code without using the “evil” eval command. I imagine this comes up under other software design pattern scenarios.

This illustrates how I imagine the method with eval:

class Database {
    public static function deadlockSafeTransaction($code) {
        do {
            // setup a condition
            try {
                // start transaction

                eval($code); //evil

                // commit transaction
            }
            catch(Exception $ex) {
                // rollback transaction
                // analyze exception for deadlock and adjust condition or rethrow exception
            }
        } while(condition);
    }
}

This illustrates an undesired coding practice if I were to use the method above:

// code

Database::deadlockSafeTransaction("
// code - sometimes a lot of complicated code with other method calls and loops
");

// code

I am looking for a solution that does not include the use of the eval command.

After writing all of this up, I just thought of the possibility of flanking my code with include statements:

include "do-try.php";
// database code
include "catch-while.php";

But I am looking for something more OOPish and less hackish. I don’t even know if the above would work and the includes may carry an unnecessary performance expense. So the issue still warrants inquiry and discussion for the best solution.

  • 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-25T23:51:03+00:00Added an answer on May 25, 2026 at 11:51 pm

    Use callbacks:

     function doInTransaction($fn) {
    
         // stuff
    
         $fn();
    
         // more stuff
    
     }
    

    and then

     doInTransaction(function() {
    
       // whatever
    
     });
    

    As to the question how to pass the context into the function, I don’t think using globals is a good idea. Much better is to pass what you need explicitly to the function:

    function doInTransaction($fn) {
    
        // stuff
    
        $fn($this->databaseConnection, $this->logFile);
    
        // more stuff
    }
    

    and the function should be prepared to accept what’s gets passed to it:

    doInTransaction(function($connection, $logFile) {
    
        $x = $connection->xxxx
    
    })
    

    Even better option is to make function a Delegate and just give it the whole “parent” object:

    function doInTransaction($delegate) {
    
        // stuff
    
        $delegate($this);
    
        // more stuff
    }
    
    doInTransaction(function($dbObject) {
    
        $dbObject->doSomething(...);
    
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have noticed issue while testing iphone app that if one quickly opens/dismisses a modal
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
Have you guys had any experiences (positive or negative) by placing your source code/solution
have a table that dynamically generates text boxes in run time. I want to
Have a build process that can't be edited and need to pack another war
I see that some rss on xml have strange strings. For example, ... is
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Have a look at this HTML code: <div class=overlay> <a href=#>1</a> <a href=#>1</a> <a
Have an app that can use tts to read text messages. It can also

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.