The mysqli Extension and Persistent Connections

The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused.

Unlike the mysql extension, mysqli does not provide a separate function for opening persistent connections. To open a persistent connection you must prepend p: to the hostname when connecting.

The problem with persistent connections is that they can be left in unpredictable states by clients. For example, a table lock might be activated before a client terminates unexpectedly. A new client process reusing this persistent connection will get the connection as is. Any cleanup would need to be done by the new client process before it could make good use of the persistent connection, increasing the burden on the programmer.

The persistent connection of the mysqli extension however provides built-in cleanup handling code. The cleanup carried out by mysqli includes:

  • Rollback active transactions

  • Close and drop temporary tables

  • Unlock tables

  • Reset session variables

  • Close prepared statements (always happens with PHP)

  • Close handler

  • Release locks acquired with GET_LOCK()

This ensures that persistent connections are in a clean state on return from the connection pool, before the client process uses them.

The mysqli extension does this cleanup by automatically calling the C-API function mysql_change_user().

The automatic cleanup feature has advantages and disadvantages though. The advantage is that the programmer no longer needs to worry about adding cleanup code, as it is called automatically. However, the disadvantage is that the code could potentially be a little slower, as the code to perform the cleanup needs to run each time a connection is returned from the connection pool.

It is possible to switch off the automatic cleanup code, by compiling PHP with MYSQLI_NO_CHANGE_USER_ON_PCONNECT defined.

Hinweis:

The mysqli extension supports persistent connections when using either MySQL Native Driver or MySQL Client Library.

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

orazepam Köp lagligt. Lorazepam ingen recept - Apoteksvarumärke Lorazepam. Stockholm

Köp lagligt Lorazepam. Lorazepam ingen recept Behöver du pålitliga, högkvalitativa mediciner, men vill inte lämna hemmet för att köpa dem? ...

Geschrieben von travelder am 20.06.2026 05:20:02
Forum: Bücher-Forum
atapres Achetez de manière anonyme. Generique Catapres - Acheter Catapres Catapres.

Achetez de manière anonyme Catapres. Generique Catapres Avez besoin de médicaments de haute qualité ? Nous sommes là pour vous ! Notre pharmac ...

Geschrieben von drawer am 20.06.2026 05:19:51
Forum: IT-Security
aleplon En línea. donde comprar Zaleplon en Europa - como hacer receta para Zaleplon

En línea Zaleplon. donde comprar Zaleplon en Europa ¿Necesita medicamentos de calidad sin complicaciones? ¡Nuestra farmacia en línea está aqu ...

Geschrieben von travelder am 20.06.2026 05:19:23
Forum: Entwicklungsumgebungen
anax Without recipe. Xanax fedex no prescription - Purchase Xanax Using Bitcoin. Mil

Without recipe Xanax. Xanax fedex no prescription Do you need reliable, high-quality medications, but don't want to leave home to buy them? Then y ...

Geschrieben von drawer am 20.06.2026 05:18:52
Forum: Ankündigungen