Ending a persistent connection in PHP

[adrotate group=”3,4,7″]
One of the questions we had when running persistent connections was how do you end them? We had been trying to use the i5_pclose() function but for some reason it did not seem to work.

We had assumed that calling the function without a connection resource would end the last connection, that it appears is wrong! You should always call the function by passing in the $conn resource you want to close. Because of the way the connection is created and managed we knew that the job which would be running any requests we made would always be the same one. Testing also showed us that even if we used the same user id to connect from multiple browsers on the same system the connection ID would be different for each bowser session.

When the sessions did not end we started to look at alternatives to the i5_pclose() function. One idea we came up with was to use the endjob *immed command, as it would be running in the same job the connection was using, we knew we could issue the command with JOB(*) as that is the current job. The following code worked and closed the job as expected.


include('functions.php');
// send a request to end the job on the target system
$conn = 0;
if(connect($conn)) {
	i5_command("ENDJOB JOB(*) OPTION(*IMMED)",$conn);
	}
$ret_url = $_SESSION['ret_url'];
session_destroy();
header('Location: ' .$ret_url);
exit(0);

We include the functions.php script as it has the connect() function in it which returns the correct connection. The ENDJOB command simply ends the current job, we then destroy any session variables which forces the user to resign in and push the user back to a sign on page.

Then the light went on, maybe the reason the i5_pclose() function was not working (it did once but we figure that was a fluke) was because we needed to pass in the connection id. As it turned out this was correct so the following code also ends the correct persistent connection.


include('functions.php');
// send a request to end the job on the target system
$conn = 0;
if(connect($conn)) {
	i5_pclose($conn);
	}
$ret_url = $_SESSION['ret_url'];
session_destroy();
header('Location: ' .$ret_url);
exit(0);

This is probably a better way to close the connection as the EasyCom server may do some clean up as it removes the connection.

Hope it helps others understand a bit more about persistent connections.

Chris…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.