13.1 The Concept of Persistent Database Connections


This section examines the concepts related to persistent database connections in PHP.

Many people think that persistent connections can be used to have one database connection during an entire session. In other words, you connect to the database when you enter an online shop and quit it when you leave the shop. This is not what persistent connections are all about. HTTP is not a connection-oriented protocol, so it is not possible to use the same database handle across more than just one screen. In addition, database handles are automatically destroyed at the end of a PHP script, so it is not possible to pass the database handle to the next screen. This is a very important point that you must keep in mind when working with PHP.

The main idea of persistent database connections is that the time needed for user authentication is reduced significantly. This goal can be achieved by "connection recycling." What does connection recycling mean? PHP keeps an internal pool of open connections related to a certain database, user, and host. Every time a user wants to connect to the database, PHP looks to see if there is a spare open connection in the pool and assigns it to the new user. If no spare connection is available, PHP attempts to establish a new connection to the database. As soon as the user disconnects, the connection won't be terminated, but it will be available in the pool of free connections again. With the help of this algorithm, many user authentication processes are saved, so a lot of performance can be gained. For users working with persistent instead of nonpersistent connections, this makes no difference in most cases. However, you will learn in this chapter that there are some things you have to take care of.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net