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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:58:51+00:00 2026-06-17T20:58:51+00:00

So I finally started to refactor the base of my PHP office app: I

  • 0

So I finally started to refactor the base of my PHP office app: I added new functionality to it based on a singleton PDO-class, which I was planning to deploy throughout the whole app. It is very straightforward and working (for now) as it should:

class DB {

    protected static $instance;
    protected function __construct() {}
    public static function getInstance()
    {

        if( empty( self::$instance ) )
        {
            $dsn =  'pgsql:host=' . Config::$a .
                    ';dbname='    . Config::$b .
                    ';port='      . Config::$c .
                    ';connect_timeout=15';            
            $db_user = Config::$d;             
            $db_pass = Config::$e;

            try
            {
                self::$instance = new PDO( $dsn, $db_user, $db_pass );
                self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            }  
            catch( PDOException $e )
            {  
                new Log( 6, "DB Class failed to connect to dbase: $e" );
            }                          
        }
        return self::$instance;
    }
}

This afternoon I’m telling a friend of mine, who is a .NET-programmer about this singleton db-connection class, and he warns me that using a singleton to connect to a database can lead to threading-problems.

I must say that it had crossed my mind: several users will be using the app, and almost all functionality is related to (multiple) db-queries. It is not unthinkable that two users will be executing a query at the same time.

Is PDO (or PHP, or Apache (at the office), or Nginx (testing environment at home)) capable of dealing with this? Maybe a new instance is given for every user? Maybe I should use this singleton together with transactions that will lock the database for every query/update/insert/delete to avoid threading problems? Or maybe my friend is wrong and I don’t have to worry?

Thx for any insights!

  • 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-17T20:58:52+00:00Added an answer on June 17, 2026 at 8:58 pm

    Do not worry about using a singleton to establish the connection.

    Every php script request will have its own thread. I mean, if two simultaneous requests happen, the web server will execute your singleton in two different threads, so you finally will have two different objects dealing with the database connection.

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

Sidebar

Related Questions

Recently I started a new app requiring just one store (no document based app).
After many months of postponing it, this week, I finally started using a new
OK, so I've finally started with math in python, which is (so far) surprisingly
Today I finally started to write some unit tests for my new ASP.net MVC
I started a new blank console app project in VS (I am using VS2010),
I've finally started making a Rails app from scratch and I'm getting the hang
I've finally started to learn PHP and I quite have trouble with it. I
I have finally started messing around with creating some apps that work with RESTful
I've started working with ASP.net AJAX (finally ☺). and I've got an update panel
Ok, so I've finally bowed to peer pressure and started using Spring in my

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.