Jump to content

Singleton and transactions


rolandpish

Recommended Posts

Hi!

I have a question about singleton and transactions in a mysql database.

Recently I built an application which uses PDO to access the mysql database and I'm using Singleton pattern to allow only one instance of the connection to the bd.

 

I use transactions in most of the database operations. So, let's show this scenario: 5 users in their own computers in 5 different pages of my application. They are ready to click a button that triggers operations (which use BD transactions).

¿Does PHP creates a DB instance for each one of these 5 users? or ¿Does PHP share the same DB instance for these 5 users?

¿How does Singleton handle the particular transaction for each user?

For instance, let's say that the 5 users click at the same time. ¿Could this possibly lead to errors on db operations?

 

I would really appreciate your help.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/192792-singleton-and-transactions/
Share on other sites

¿Does PHP creates a DB instance for each one of these 5 users?

yes

¿Does PHP share the same DB instance for these 5 users?

no

¿How does Singleton handle the particular transaction for each user? For instance, let's say that the 5 users click at the same time. ¿Could this possibly lead to errors on db operations?

 

It won't. MySQL transactions are ACID compliant, which means they are dwsigned to handle such situations without errors. If requests of one user (A) will make it unable for another user (B) to perform his actions, the transaction for user B should be rolled back (note that you should put this in your code by yourself - query fails -> rollback)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.