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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:23:40+00:00 2026-06-01T03:23:40+00:00

Lets put the case that we’ve got two separate classes, each owns a mysqli

  • 0

Lets put the case that we’ve got two separate classes, each owns a mysqli object to use for database operations.

class A{
  protected $mysqliObject;
  public function __constructor(){
    $this->mysqliObject=new mysqli(...)
  }
  public function doSomething(){
    $this->mysqliObject->query(...);
  }
}
class B{
  protected $mysqliObject;
  public function __constructor(){
    $this->mysqliObject=new mysqli(...)
  }
  public function doSomething(){
    $this->mysqliObject->query(...);
  }
}

The doSomething() methods use the $mysqliObject for a database query (SELECT, UPDATE, DELETE, etc…). I want to do the following outside:

// Start point
$aObject=new A();
$bObject=new B();
$aObject->doSomething();
$bObject->doSomething();
// End point

…but I want to roll back both queries if any of them fails. How can I do that?

  • Can I create a brand new mysqli object at “Start point”, and use it to commit or rollback at “End point” the database query of class A and class B?
  • Do I have to create outside, and pass the same $mysqliObject to the __constructor() of class A and class B, and use that to rollback or commit (outside).

Would like to see more techniques with pro-con’s.

  • 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-01T03:23:41+00:00Added an answer on June 1, 2026 at 3:23 am

    Each database connection object you have represents a different connection to the database, even if the connection is to the same database server. Each connection is completely independent of each other, so a transaction started in one connection will be invisible to the other connection.

    If all your database access is to the same server, then just create a single connection and pass it to anything that needs access to the connection. Then the transaction will be application-wide.

    class ClassThatNeedsDbAccess
    {
        protected $connection = NULL;
    
        protected function doQuery ($query)
        {
            return ($this -> connection -> execute ($query));
        }
    
        public function __construct (PDO $connection)
        {
            $this -> connection = $connection;
        }
    }
    
    class OtherClassThatNeedsDbAccess extends ClassThatNeedsDbAccess
    {
    }
    
    $myDb = new PDO ('dsn_goes_here');
    
    $obj1 = new ClassThatNeedsDbAccess ($myDb);
    $obj2 = new OtherClassThatNeedsDbAccess ($myDb);
    

    If you’re talking to different database servers, then you probably should wrap all the connections in an object that can coordinate the database activity. It will be that object’s responsibility to track which database connection has a transaction in flight.

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

Sidebar

Related Questions

Lets put it in parts. I got a socket receiving data OK and I
Lets just put it at its simplest, a table with two fields: 'item_id' &
I am looking into a basic DOM layout that lets me put a couple
I've got a huge (>>10m) list of entries. Each entry offers two hash functions:
Lets say I have a Label, Button, or TextArea object, that contains some amount
I want to put a conditional breakpoint in windbg. For example lets say LoadLibrary
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
I have code already that loops through each member of my website and can
In my login script I have put a checkbox that people can check if
Let's assume I have two entities - project team and employee. Each employee could

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.