File: persistconns.xml

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (141 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="mysqli.persistconns" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">

 <title>The mysqli Extension and Persistent Connections</title>

 <para>
  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.
 </para>
 
 <para>
  Unlike the mysql extension, mysqli does not provide a separate function
  for opening persistent connections. To open a persistent connection you
  must prepend <literal>p:</literal> to the hostname when connecting.
 </para>

 <para>
  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
  <quote>as is</quote>. 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.
 </para>

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

 <itemizedlist>

  <listitem>
   <para>
    Rollback active transactions
   </para>
  </listitem>

  <listitem>
   <para>
    Close and drop temporary tables
   </para>
  </listitem>

  <listitem>
   <para>
    Unlock tables
   </para>
  </listitem>

  <listitem>
   <para>
    Reset session variables
   </para>
  </listitem>

  <listitem>
   <para>
    Close prepared statements (always happens with PHP)
   </para>
  </listitem>

  <listitem>
   <para>
    Close handler
   </para>
  </listitem>

  <listitem>
   <para>
    Release locks acquired with <function>GET_LOCK</function>
   </para>
  </listitem>

 </itemizedlist>

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

 <para>
  The <literal>mysqli</literal> extension does this cleanup by
  automatically calling the C-API function
  <literal>mysql_change_user()</literal>.
 </para>

 <para>
  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 <emphasis>potentially</emphasis>
  be a little slower, as the code to perform the cleanup needs to run
  each time a connection is returned from the connection pool.
 </para>

 <para>
  It is possible to switch off the automatic cleanup code, by compiling
  PHP with

  <constant>MYSQLI_NO_CHANGE_USER_ON_PCONNECT</constant>

  defined.
 </para>

 <note>
  <para>
   The <literal>mysqli</literal> extension supports persistent
   connections when using either MySQL Native Driver or MySQL Client
   Library.
  </para>
 </note>

</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->