This module manages database connections, in a manner
optimised for the platform. On non-threaded platforms,
it provides a persistent connection in the manner of
classic LAMP (Linux, Apache, Mysql, Perl/PHP/Python).
On threaded platform, it provides an altogether more
scalable and efficient connection pool, as
described in this
article at ApacheTutor. Note that
To connect to your database, you'll need to specify a driver, and connection parameters. These vary from one database engine to another. For example, to connect to mysql, do the following:
You can then use this connection in a variety of other
modules, including
See
prepared
field of an ap_dbd_t
.
Hash entries are of type apr_dbd_prepared_t
and can be used in any of the apr_dbd prepared statement
SQL query or select commands.
It is up to dbd user modules to use the prepared statements
and document what statements can be specified in httpd.conf,
or to provide their own directives and use ap_dbd_prepare
.
reconnect
to 0 in the connection string as to avoid errors that
arise from the MySQL client reconnecting without properly resetting the
prepared statements. If set to 1, any broken connections will be attempted
fixed, but as mod_dbd is not informed, the prepared statements will be invalidated.
Any web/database application needs to secure itself against SQL injection attacks. In most cases, Apache DBD is safe, because applications use prepared statements, and untrusted inputs are only ever used as data. Of course, if you use it via third-party modules, you should ascertain what precautions they may require.
However, the FreeTDS driver is inherently unsafe. The underlying library doesn't support prepared statements, so the driver emulates them, and the untrusted input is merged into the SQL statement.
It can be made safe by untainting all inputs: a process inspired by Perl's taint checking. Each input is matched against a regexp, and only the match is used, according to the Perl idiom:
$untrusted =~ /([a-z]+)/;
$trusted = $1;
To use this, the untainting regexps must be included in the prepared statements configured. The regexp follows immediately after the % in the prepared statement, and is enclosed in curly brackets {}. For example, if your application expects alphanumeric input, you can use:
"SELECT foo FROM bar WHERE input = %s"
with other drivers, and suffer nothing worse than a failed query. But with FreeTDS you'd need:
"SELECT foo FROM bar WHERE input = %{([A-Za-z0-9]+)}s"
Now anything that doesn't match the regexp's $1 match is discarded, so the statement is safe.
An alternative to this may be the third-party ODBC driver, which offers the security of genuine prepared statements.
Selects an apr_dbd driver by name. The driver must be installed
on your system (on most systems, it will be a shared object or dll).
For example, DBDriver mysql
will select the MySQL
driver in apr_dbd_mysql.so.
As required by the underlying driver. Typically this will be used to pass whatever cannot be defaulted amongst username, password, database name, hostname and port number for connection.
Connection string parameters for current drivers include:
PQconnectdb
part1:part2
is used as sqlite_open(part1, atoi(part2), NULL)
sqlite3_open
If set to Off, persistent and pooled connections are disabled. A new database connection is opened when requested by a client, and closed immediately on release. This option is for debugging and low-usage servers.
The default is to enable a pool of persistent connections (or a single LAMP-style persistent connection in the case of a non-threaded server), and should almost always be used in operation.
Prior to version 2.2.2, this directive accepted only the values
0
and 1
instead of Off
and
On
, respectively.
For modules such as authentication that repeatedly use a single SQL statement, optimum performance is achieved by preparing the statement at startup rather than every time it is used. This directive prepares an SQL statement and assigns it a label.
Set the minimum number of connections per process (threaded platforms only).
Set the maximum number of connections per process to be sustained, other than for handling peak demand (threaded platforms only).
Set the hard maximum number of connections per process (threaded platforms only).
Set the time to keep idle connections alive when the number of connections specified in DBDKeep has been exceeded (threaded platforms only).
Modules, that wish it, can have one or more SQL statements executed when a connection to a database is created. Example usage could be initializing certain values or adding a log entry when a new connection is made to the database.