Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Need help with MySQLi Transaction spaghetti hell

Codes here !

Moderators: macek, egami, gesf

Need help with MySQLi Transaction spaghetti hell

Postby sccy » Wed Jun 26, 2013 7:35 pm

I have got a class with more than a hundred functions/methods in it. Many of them require to run MySQLi query within a transaction. So, in a simple form, most of the functions look like this:

Code: Select all
class MyClass {
    private $connection = ...; // mysqli connection
    public function a () {
        $this->connection->autocommit(FALSE);
        // do something and run multiple queries
        $this->connection->commit();
    }
    public function b () {
        $this->connection->autocommit(FALSE);
        // do something and run multiple queries
        $this->connection->commit();
    }
}
// to run the code:
$myclass = new MyClass();
$myclass->a(); // all queries are run inside a transaction
$myclass->b(); // all queries are run inside a transaction


The concept is simple: Every time I run a public function/method, it starts a transaction and commits before ending the function/method.
Now, my problem is... what if I want to run a method within another method, while keeping both methods public and callable by callers outside the class??? For example:

Code: Select all
class MyClass {
    private $connection = ...; // mysqli connection
    public function a () {
        $this->connection->autocommit(FALSE);
        // do something and run multiple queries
        $this->connection->commit();
    }
    public function b () {
        $this->connection->autocommit(FALSE);
        // do something and run multiple queries
        $this->a(); // running function a inside function b here
        $this->connection->commit();
    }
}
// to run the code:
$myclass = new MyClass();
$myclass->a(); // all queries are run inside a transaction
$myclass->b(); // transaction is not working as I initially wished as this function consists of more than one commits.


Using extra variables for manual if/else checking, I suppose I can solve the problem above with two functions. However, my problem here is that I have multiple, possibly nearly a hundred, functions/methods that act like the above. I don't think I can use a few if/else to handle such complicated problem of transaction spaghetti hell.

Anyone has any ideas to help me out a bit??? Any sound suggestions and helps will be truly appreciated.
sccy
New php-forum User
New php-forum User
 
Posts: 1
Joined: Wed Jun 26, 2013 7:15 pm

Re: Need help with MySQLi Transaction spaghetti hell

Postby johnj » Wed Jul 03, 2013 5:43 am

Use switch case.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron