We help IT Professionals succeed at work.

PHP login as Singleton design pattern

Hi,

I have a PHP login class set up with the Singleton design pattern. I'm wondering if I have multiple users logging and updating using this class will I encounter any problems with number of connections (since it's a Singleton design pattern).

Should I create a PHP login system with a Registry Pattern instead. I noticed that one limitation I had with the Singleton design pattern is that I set the database in it and so for a user to use another database then I had to create another instance of the connection.

I'm wondering if using mysqli is more efficient than PDO. I coded it with PDO. Mysqli has the function to end the connection. I don't know how PDO handles it though. Right now I don't have a lot of users but I want to be able use my login code that will scale well.

---

I guess in a related way the number of MySQL connections is set in my.conf but I'm not certain. Any suggestions? How would I test that my class can handle x number of users of logins?

Thanks,
Victor
Comment
Watch Question

Something to remember is this: each page request is single person, so unless you require access to multiple databases, Singleton is fine.  If there begins to be problems with the number of connections, then using connection pooling (something outside of the scope of PHP) should suffice (though I'm not directly familiar with this as it pertains to MySQL).

What I've been using for about a decade now is an abstraction layer for the database.  I can create as many instances of the object (each with it's own connection) as necessary.  One object per database seems to be pretty safe, passing that object to other objects which interact with the given database.

Most of my application use a single database.  I create a database object, and then pass that reference into other objects that require it.  The biggest problem I've encountered is simply with those objects being aware of whether or not they properly started/ended a transaction (e.g. one object creates the connection, puts it into a transaction, then fails to commit/rollback prior to finishing with it).

Hope that helps.
Victor KimuraSEO, Web Developer

Author

Commented:
Hi crazedsanity,

I'm curious as to how you resolved the problem with the transactions. I assume you're using the db abstraction that allows multiple instances of the object (each with its own connection).

1) Do you mind sharing that abstraction or is it something you don't wish to share?

For multiple databases I used that Singleton and created a Registry out of it to allow multiple db connections. But I don't know if that will scale well.

2) Does your class handle large number of simultaneous connections? Does it scale well?

I'm just thinking that if I use my class (Singleton) I don't know if it will scale well.

3) Are there programs that will test my PHP code and web server for stress testing that you've used. I develop on my Windows (WAMP) primarily but have a local Centos to use (LAMP).

Thanks,
Victor
The abstraction layer that I use is a part of a couple of open source libraries that I (mostly) wrote and presently maintain.  The library that has the database abstraction layer is available for free (best to download via Subversion): [ https://cs-webapplibs.svn.sourceforge.net/svnroot/cs-webapplibs/trunk/current/ ], with the base required library (at least I'm pretty sure it's required, anyway): [ https://cs-content.svn.sourceforge.net/svnroot/cs-content/trunk/current ].

The library itself is the "cs_PHPDB.class.php".  It (primarily) supports PostgreSQL, with MySQL and SQLite as well.  I haven't done any testing as to how it works in Windows... I tried briefly in the past, but really had no reason or time to do much more than that.

The transaction issue is something that I've mostly dealt with in PostgreSQL, as it has much stronger support; MySQL can have pretty flaky transactional support, since that requires InnoDB tables, and a single database can have a mixture of MyISAAM, InnoDB, and others.

As for multiple database connections... I've written an application in the past that connected to six or seven different databases, across two servers (I think).  I made a lot of improvements to the library while working on that particular app, and it seemed to perform quite well.
Those libraries are fairly old, originally for PHP4 and then migrated to PHP5 at some point.  If you're experienced with PHP and dig into them at all, you'll probably find some weirdness scattered throughout, especially in the oldest classes.

For testing, I use "simpletest" version 1.0.1; there's actually a "tests" folder that does some rudimentary testing of the classes.  I believe all the tests are based around the PostgreSQL layer... I'm not all that fond of MySQL nor SQLite.

I haven't really done much in the way of load testing.  In my experience (or maybe it's just a biased opinion), problems with too many connections or anything database-related can all be handled at the database level--especially true when using a network DBMS like PostgreSQL or MySQL.
Victor KimuraSEO, Web Developer

Author

Commented:
Hi crazedsanity,

Thanks for the reply. I assume when you are referring to old libraries it is the svn depositories, right?

I prefer not to use old libraries which it seem like they are around but I guess I have to work with something and develop it from there.

Have you uploaded your libraries that you have been working on? If it's proprietary code then that's okay.

I understand your point of view of PostgreSQL. I'm just more familiar with MySQL right now. When I have time I'll delve more into the former for sure. Are there good support forums of PostgreSQL?

Thanks,
Victor
When I was referring to old libraries, I was referring to those in the SVN repositories.  They've been around for quite some time, and have stood the test of time; they're not broken nor poorly written, they simply have not gone through any massive rewrites that would utilize newer capabilities present in PHP.  Most of the reason for this (besides lack of time) is that, in theory, they'll work for a wider array of PHP versions.

PostgreSQL has many very good support forums.  There's at least one company that provides paid support, along with several others that have database applications based on it.
Victor KimuraSEO, Web Developer

Author

Commented:
What is the PostgreSQL support forum (paid and unpaid ones). When I have more time I'll delve into PostgreSQL more.

Thanks,
Victor
The PostgreSQL site has a list of professional services on their site:
  * Paid [http://www.postgresql.org/support/professional_support_northamerica]
  * Community (free): [http://www.postgresql.org/community/]

The companies that do support or are based on PostgreSQL (that I can think of):
  * Command Prompt [http://www.commandprompt.com/]
  * EnterpriseDB [http://enterprisedb.com/]

The main feature that I absolutely adore about PostgreSQL that seems to be lacking in other free databases is that schema changes can be made within a transaction (and rolled-back).  This is a huge plus for me when testing automated upgrade scripts using the "Web DB Upgrade" system ([https://cs-webapplibs.svn.sourceforge.net/svnroot/cs-webapplibs/trunk/current/cs_webdbupgrade.class.php]), so that all schema & data changes can happen at once without having to worry as much about backing-up the database or having to restore it if the script bombs.
Victor KimuraSEO, Web Developer

Author

Commented:
Thank you for the input. Valuable info.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.