简体   繁体   English

维护php和Java应用之间的会话/连接

[英]maintain session/connection between php and java app

A) If I have a java application which starts up and waits for socket connections on a specified port. A)如果我有一个Java应用程序启动,并等待指定端口上的套接字连接。 Is there a way i could maintain a session connection in php after a user validates, ie a persistent connection? 有没有一种方法可以在用户验证后在php中维护会话连接,即持久连接?

B) I mean, I'm trying to understand how its done with mysql and php. B)我的意思是,我想了解它是如何通过mysql和php完成的。 How does mysql or php know what the last opened connection was so you don't have to do mysql_connect after ? mysql或php如何知道上次打开的连接是什么,因此您不必在之后执行mysql_connect?

C) Is there any benefit to opening and closing a connection on each page load or is it better to maintain a persistent connection? C)在每次页面加载时打开和关闭连接是否有任何好处,还是保持持久连接更好?

D) If the latter is true in C, can you describe or give an example of how it would be achieved in a php --> Java connection D)如果后者在C中是正确的,您能否描述或给出如何在php-> Java连接中实现的示例

A) No, there isn't. A)不,没有。

B) mysql_pconnect() works because of how the web server and php cooperates. B)mysql_pconnect()之所以有效,是因为Web服务器和php如何协作。 The web server will typically launch a number of child processes which handles request. Web服务器通常会启动许多处理请求的子进程。 Each child process can only handle a single request at a time, and concurrency is achieved by sending concurrent requests to different processes. 每个子进程一次只能处理一个请求,并发是通过将并发请求发送到不同的进程来实现的。

Each such process has its own instance of PHP, which is reused for each new request. 每个此类进程都有其自己的PHP实例,可将其用于每个新请求。 This allows PHP modules to maintain some state between requests. 这使PHP模块可以在请求之间保持某些状态。 You cannot do this from regular PHP code, it has to be an extension written in C. There's no guarantees about this, though. 您不能通过常规的PHP代码来执行此操作,它必须是用C编写的扩展。但是,对此不能保证。 A process can be killed and relaunched at any time. 可以随时终止进程并重新启动它。

Sidenote: Of course, not all web servers uses processes like this. 旁注:当然,并非所有的Web服务器都使用这样的过程。 It's rather common to use a threaded approach instead. 相反,通常使用线程化方法。 This doesn't work on PHP, though, since not all extensions are thread safe. 但是,这在PHP上不起作用,因为并非所有扩展都是线程安全的。 Therefore PHP always has to run on a web server that creates child processes to handle requests. 因此,PHP始终必须在创建子进程以处理请求的Web服务器上运行。 This mode (MPM) is called prefork on Apache. 这种模式(MPM)在Apache上称为prefork。

C) As I said, you don't have the choice. C)正如我说的,您别无选择。 On a fast network, the overhead for opening a new connection is quite small, though. 但是,在快速网络上,打开新连接的开销非常小。

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

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