简体   繁体   English

单例database.php

[英]Singleton database.php

If I have a database.php class (singleton) which reads and writes information for users of my web application, what happens when simultaneous requests for the same database function is called? 如果我有一个database.php类(单例),该类为Web应用程序的用户读取和写入信息,那么当同时请求同一数据库功能时会发生什么?

Is it possible that the database class will return the wrong information to other users accessing the same function at the same time? 数据库类是否有可能将错误的信息返回给同时访问同一功能的其他用户?

What other similar problems could occur? 还有什么其他类似的问题?

what happens when simultaneous requests for the same database function is called? 当同时请求同一数据库功能时会发生什么? Is it possible that the database class will return the wrong information to other users accessing the same function at the same time? 数据库类是否有可能将错误的信息返回给同时访问同一功能的其他用户?

Absolutely not. 绝对不。

Each PHP request is handled entirely in it's own process space. 每个PHP请求都完全在其自己的进程空间中处理。 There is no threading, no application server connection pool, no shared memory, nothing funky like that. 没有线程,没有应用程序服务器连接池,没有共享内存,没有任何时髦的东西。 Nothing is shared unless you've gone out of your way to do so (like caching things in APC/memcached). 除非您全力以赴,否则什么都不会共享(例如在APC / memcached中缓存内容)。

Every time the application starts, your Singleton will get created. 每次启动应用程序时,都会创建您的Singleton。 When the request ends, so does the script. 当请求结束时,脚本也将结束。 When the script exits, all of the variables, including your Singleton, go away with it. 脚本退出时,所有变量(包括您的Singleton)都将消失。

What other similar problems could occur? 还有什么其他类似的问题?

Unless you are using transactions (and if you're using MySQL, using a transaction-safe table type like InnoDB), it is possible that users could see partial updates. 除非您使用事务(如果您使用MySQL,并且使用事务安全表类型(如InnoDB)),则用户可能会看到部分更新。 For example, let's say that you need to perform an update to three tables to update one set of data properly. 例如,假设您需要对三个表执行更新以正确更新一组数据。 After the first update has completed but before the other two have completed, it's possible for another process to come along and request data from the three tables and grab the now inconsistent data. 在第一个更新完成之后,但在其他两个更新完成之前,可能需要进行另一个过程,并从三个表中请求数据并获取现在不一致的数据。 This is one form of race condition . 这是种族条件的一种形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM