PHP Session死锁现象与成因分析
近期在开发过程中遇到一个棘手的技术难题。
当后台正在异步处理大量数据时,其他控制器的请求均无法返回响应信息。起初怀疑是Ext框架配置了Ajax同步请求所致。
经排查,确认为Session死锁导致其他控制器必须等待当前Session操作完成方可继续执行,这主要源于用户登录判断逻辑中需要更新Session数据。
当PHP脚本处理耗时的大数据量操作未能及时结束时,若此时有其他访问请求进入其他控制器,
便极易引发Session死锁现象。
经过与同事探讨,确定可以使用session_write_close()函数来解决这一困境。
具体实现代码如下:
描述
void session_write_close ( void )
终止当前会话并将Session数据持久化存储。
Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.
功能说明: 终止当前的Session操作并保存Session数据。
官方文档解释得很清楚,当脚本请求未调用session_write_close();时,虽然Session数据最终会被存储,但Session Data(即Session文件)处于锁定状态。
这种锁定机制旨在防止其他应用在此时操作Session从而引发不必要的后果。在使用框架时,不同文件可能会频繁操作Session,为避免其他操作对当前Session产生死锁等待,
可以调用此函数...