switch to a 60 bit hash
[httpd-crcsyncproxy.git] / docs / manual / mod / mod_dbd.xml
blob7144a1fcef7b4e4c48b79e897c1030d44de81af8
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
14      http://www.apache.org/licenses/LICENSE-2.0
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
23 <modulesynopsis metafile="mod_dbd.xml.meta">
25 <name>mod_dbd</name>
26 <description>Manages SQL database connections</description>
27 <status>Extension</status>
28 <sourcefile>mod_dbd.c</sourcefile>
29 <identifier>dbd_module</identifier>
30 <compatibility>Version 2.1 and later</compatibility>
32 <summary>
33     <p><module>mod_dbd</module> manages SQL database connections using
34     <glossary>APR</glossary>.  It provides database connections on request
35     to modules requiring SQL database functions, and takes care of
36     managing databases with optimal efficiency and scalability
37     for both threaded and non-threaded MPMs.  For details, see the
38     <a href="http://apr.apache.org/">APR</a> website and this overview of the
39     <a href="http://people.apache.org/~niq/dbd.html">Apache DBD Framework</a>
40     by its original developer.
41 </p>
42 </summary>
44 <seealso><a href="../misc/password_encryptions.html">Password Formats</a></seealso>
46 <section id="pooling"><title>Connection Pooling</title>
47     <p>This module manages database connections, in a manner
48     optimised for the platform.  On non-threaded platforms,
49     it provides a persistent connection in the manner of
50     classic LAMP (Linux, Apache, Mysql, Perl/PHP/Python).
51     On threaded platform, it provides an altogether more
52     scalable and efficient <em>connection pool</em>, as
53     described in <a href="http://www.apachetutor.org/dev/reslist">this
54     article at ApacheTutor</a>.  Note that <module>mod_dbd</module>
55     supersedes the modules presented in that article.</p>
56 </section>
58 <section id="API"><title>Apache DBD API</title>
59     <p><module>mod_dbd</module> exports five functions for other modules
60     to use. The API is as follows:</p>
62     <example>
63 <pre><code>typedef struct {
64     apr_dbd_t *handle;
65     apr_dbd_driver_t *driver;
66     apr_hash_t *prepared;
67 } ap_dbd_t;
69 /* Export functions to access the database */
71 /* acquire a connection that MUST be explicitly closed.
72  * Returns NULL on error
73  */
74 AP_DECLARE(ap_dbd_t*) ap_dbd_open(apr_pool_t*, server_rec*);
76 /* release a connection acquired with ap_dbd_open */
77 AP_DECLARE(void) ap_dbd_close(server_rec*, ap_dbd_t*);
79 /* acquire a connection that will have the lifetime of a request
80  * and MUST NOT be explicitly closed.  Return NULL on error.
81  * This is the preferred function for most applications.
82  */
83 AP_DECLARE(ap_dbd_t*) ap_dbd_acquire(request_rec*);
85 /* acquire a connection that will have the lifetime of a connection
86  * and MUST NOT be explicitly closed.  Return NULL on error.
87  */
88 AP_DECLARE(ap_dbd_t*) ap_dbd_cacquire(request_rec*);
90 /* Prepare a statement for use by a client module */
91 AP_DECLARE(void) ap_dbd_prepare(server_rec*, const char*, const char*);
93 /* Also export them as optional functions for modules that prefer it */
94 APR_DECLARE_OPTIONAL_FN(ap_dbd_t*, ap_dbd_open, (apr_pool_t*, server_rec*));
95 APR_DECLARE_OPTIONAL_FN(void, ap_dbd_close, (server_rec*, ap_dbd_t*));
96 APR_DECLARE_OPTIONAL_FN(ap_dbd_t*, ap_dbd_acquire, (request_rec*));
97 APR_DECLARE_OPTIONAL_FN(ap_dbd_t*, ap_dbd_cacquire, (conn_rec*));
98 APR_DECLARE_OPTIONAL_FN(void, ap_dbd_prepare, (server_rec*, const char*, const char*));
99 </code></pre>
100     </example>
101 </section>
103 <section id="prepared"><title>SQL Prepared Statements</title>
104     <p><module>mod_dbd</module> supports SQL prepared statements on behalf
105     of modules that may wish to use them.  Each prepared statement
106     must be assigned a name (label), and they are stored in a hash:
107     the <code>prepared</code> field of an <code>ap_dbd_t</code>.
108     Hash entries are of type <code>apr_dbd_prepared_t</code>
109     and can be used in any of the apr_dbd prepared statement
110     SQL query or select commands.</p>
112     <p>It is up to dbd user modules to use the prepared statements
113     and document what statements can be specified in httpd.conf,
114     or to provide their own directives and use <code>ap_dbd_prepare</code>.</p>
115 </section>
117 <section id="security">
118 <title>SECURITY WARNING</title>
119     <p>Any web/database application needs to secure itself against SQL
120     injection attacks.  In most cases, Apache DBD is safe, because
121     applications use prepared statements, and untrusted inputs are
122     only ever used as data.  Of course, if you use it via third-party
123     modules, you should ascertain what precautions they may require.</p>
124     <p>However, the <var>FreeTDS</var> driver is inherently
125     <strong>unsafe</strong>.  The underlying library doesn't support
126     prepared statements, so the driver emulates them, and the
127     untrusted input is merged into the SQL statement.</p>
128     <p>It can be made safe by <em>untainting</em> all inputs:
129     a process inspired by Perl's taint checking.  Each input
130     is matched against a regexp, and only the match is used,
131     according to the Perl idiom:</p>
132     <example>
133         <pre><code>  $untrusted =~ /([a-z]+)/;
134   $trusted = $1;</code></pre>
135     </example>
136     <p>To use this, the untainting regexps must be included in the
137     prepared statements configured.  The regexp follows immediately
138     after the % in the prepared statement, and is enclosed in
139     curly brackets {}.  For example, if your application expects
140     alphanumeric input, you can use:</p>
141     <example>
142        <code>"SELECT foo FROM bar WHERE input = %s"</code>
143     </example>
144     <p>with other drivers, and suffer nothing worse than a failed query.
145     But with FreeTDS you'd need:</p>
146     <example>
147        <code>"SELECT foo FROM bar WHERE input = %{([A-Za-z0-9]+)}s"</code>
148     </example>
149     <p>Now anything that doesn't match the regexp's $1 match is
150     discarded, so the statement is safe.</p>
151     <p>An alternative to this may be the third-party ODBC driver,
152     which offers the security of genuine prepared statements.</p>
153 </section>
154 <directivesynopsis>
155 <name>DBDriver</name>
156 <description>Specify an SQL driver</description>
157 <syntax>DBDriver <var>name</var></syntax>
158 <contextlist><context>server config</context><context>virtual host</context>
159 </contextlist>
161 <usage>
162     <p>Selects an apr_dbd driver by name.  The driver must be installed
163     on your system (on most systems, it will be a shared object or dll).
164     For example, <code>DBDriver mysql</code> will select the MySQL
165     driver in apr_dbd_mysql.so.</p>
166 </usage>
167 </directivesynopsis>
169 <directivesynopsis>
170 <name>DBDParams</name>
171 <description>Parameters for database connection</description>
172 <syntax>DBDParams
173 <var>param1</var>=<var>value1</var>[,<var>param2</var>=<var>value2</var>]</syntax>
174 <contextlist><context>server config</context><context>virtual host</context>
175 </contextlist>
177 <usage>
178     <p>As required by the underlying driver.  Typically this will be
179     used to pass whatever cannot be defaulted amongst username,
180     password, database name, hostname and port number for connection.</p>
181     <p>Connection string parameters for current drivers include:</p>
182     <dl>
183     <dt>FreeTDS (for MSSQL and SyBase)</dt>
184     <dd>username, password, appname, dbname, host, charset, lang, server</dd>
185     <dt>MySQL</dt>
186     <dd>host, port, user, pass, dbname, sock, flags, fldsz, group, reconnect</dd> 
187     <dt>Oracle</dt>
188     <dd>user, pass, dbname, server</dd> 
189     <dt>PostgreSQL</dt>
190     <dd>The connection string is passed straight through to <code>PQconnectdb</code></dd>
191     <dt>SQLite2</dt>
192     <dd>The connection string is split on a colon, and <code>part1:part2</code> is used as <code>sqlite_open(part1, atoi(part2), NULL)</code></dd>
193     <dt>SQLite3</dt>
194     <dd>The connection string is passed straight through to <code>sqlite3_open</code></dd>
195     <dt>ODBC</dt>
196     <dd>datasource, user, password, connect, ctimeout, stimeout, access, txmode, bufsize</dd>
197     </dl>
198 </usage>
199 </directivesynopsis>
201 <directivesynopsis>
202 <name>DBDPersist</name>
203 <description>Whether to use persistent connections</description>
204 <syntax>DBDPersist On|Off</syntax>
205 <contextlist><context>server config</context><context>virtual host</context>
206 </contextlist>
208 <usage>
209     <p>If set to Off, persistent and pooled connections are disabled.
210     A new database connection is opened when requested by a client,
211     and closed immediately on release.  This option is for debugging
212     and low-usage servers.</p>
214     <p>The default is to enable a pool of persistent connections
215     (or a single LAMP-style persistent connection in the case of a
216     non-threaded server), and should almost always be used in operation.</p>
218     <p>Prior to version 2.2.2, this directive accepted only the values
219     <code>0</code> and <code>1</code> instead of <code>Off</code> and
220     <code>On</code>, respectively.</p>
221 </usage>
222 </directivesynopsis>
224 <directivesynopsis>
225 <name>DBDPrepareSQL</name>
226 <description>Define an SQL prepared statement</description>
227 <syntax>DBDPrepareSQL <var>"SQL statement"</var> <var>label</var></syntax>
228 <contextlist><context>server config</context><context>virtual host</context>
229 </contextlist>
231 <usage>
232     <p>For modules such as authentication that repeatedly use a
233     single SQL statement, optimum performance is achieved by preparing
234     the statement at startup rather than every time it is used.
235     This directive prepares an SQL statement and assigns it a label.</p>
236 </usage>
237 </directivesynopsis>
239 <directivesynopsis>
240 <name>DBDMin</name>
241 <description>Minimum number of connections</description>
242 <syntax>DBDMin <var>number</var></syntax>
243 <default>DBDMin 1</default>
244 <contextlist><context>server config</context><context>virtual host</context>
245 </contextlist>
247 <usage>
248     <p>Set the minimum number of connections per process (threaded
249     platforms only).</p>
250 </usage>
251 </directivesynopsis>
253 <directivesynopsis>
254 <name>DBDKeep</name>
255 <description>Maximum sustained number of connections</description>
256 <syntax>DBDKeep <var>number</var></syntax>
257 <default>DBDKeep 2</default>
258 <contextlist><context>server config</context><context>virtual host</context>
259 </contextlist>
261 <usage>
262     <p>Set the maximum number of connections per process to be
263     sustained, other than for handling peak demand (threaded
264     platforms only).</p>
265 </usage>
266 </directivesynopsis>
268 <directivesynopsis>
269 <name>DBDMax</name>
270 <description>Maximum number of connections</description>
271 <syntax>DBDMax <var>number</var></syntax>
272 <default>DBDMax 10</default>
273 <contextlist><context>server config</context><context>virtual host</context>
274 </contextlist>
276 <usage>
277     <p>Set the hard maximum number of connections per process
278     (threaded platforms only).</p>
279 </usage>
280 </directivesynopsis>
282 <directivesynopsis>
283 <name>DBDExptime</name>
284 <description>Keepalive time for idle connections</description>
285 <syntax>DBDExptime <var>time-in-seconds</var></syntax>
286 <default>DBDExptime 300</default>
287 <contextlist><context>server config</context><context>virtual host</context>
288 </contextlist>
290 <usage>
291     <p>Set the time to keep idle connections alive when the number
292     of connections specified in DBDKeep has been exceeded (threaded
293     platforms only).</p>
294 </usage>
295 </directivesynopsis>
297 </modulesynopsis>