views:

24

answers:

1

A) If I have a java application which starts up and waits for socket connections on a specified port. Is there a way i could maintain a session connection in php after a user validates, i.e a persistent connection?

B) I mean, I'm trying to understand how its done with mysql and php. How does mysql or php know what the last opened connection was so you don't have to do mysql_connect after ?

C) Is there any benefit to opening and closing a connection on each page load or is it better to maintain a persistent connection?

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

+1  A: 

A) No, there isn't.

B) mysql_pconnect() works because of how the web server and php cooperates. The web server will typically launch a number of child processes which handles request. 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. This allows PHP modules to maintain some state between requests. You cannot do this from regular PHP code, it has to be an extension written in C. There's no guarantees about this, though. A process can be killed and relaunched at any time.

Sidenote: Of course, not all web servers uses processes like this. It's rather common to use a threaded approach instead. This doesn't work on PHP, though, since not all extensions are thread safe. Therefore PHP always has to run on a web server that creates child processes to handle requests. This mode (MPM) is called prefork on Apache.

C) As I said, you don't have the choice. On a fast network, the overhead for opening a new connection is quite small, though.

Emil H
Thanks for the clear and detailed response.
robinsonc494